使用smarty流程
html模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>{$title}</h1>
<p>{$content}</p>
</body>
</html>
php编译
<?php
/*
smarty使用流程:
1.引入smarty
2.实例化smarty
3.配置(最基本的要配置模板目录和编译目录)
*/
//引入Smarty
require ('../smarty3/libs/Smarty.class.php');
//实例化
$smarty = new Smarty();
// print_r($smarty);
//配置
$smarty->template_dir = './temp'; //自动创建目录
$smarty->compile_dir = './comp';
$title = '两会召开中';
$content = '提案特别多,听说房子要涨价';
//赋值
$smarty->assign('title',$title); // 找到news.html对应的便签,即$title
$smarty -> assign('content',$content );
$smarty->display('news.html');
?>
数组传值
<?php
/*
smarty使用流程:
1.引入smarty
2.实例化smarty
3.配置(最基本的要配置模板目录和编译目录)
*/
//引入Smarty
require ('../smarty3/libs/Smarty.class.php');
//实例化
$smarty = new Smarty();
// print_r($smarty);
//配置
$smarty->template_dir = './temp'; //自动创建目录
$smarty->compile_dir = './comp';
// 从数据库取出会员信息,往往是数组形式
$user = array ('name'=>'刘备','age'=>'28', 'weapon' => '双剑'); // => 是数组成员访问符号 -> 是对象成员访问符号
$smarty -> assign('name', $user['name']); // 看到麻烦的地方就要想解决方案
$smarty -> assign('age', $user['age']);
$smarty -> assign('weapon', $user['weapon']);
$zF = array('name'=>'张飞','age'=>'25', 'weapon' => '丈八蛇矛');
$smarty->assign('zF',$zF);
$gy = array(0=>'关羽',1=>'25', 'weapon'=>'青龙偃月刀');
$smarty->assign('gy',$gy);
$smarty->display('liubei.html');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table>
<tr><td>姓名</td><td>{$name}</td></tr>
<tr><td>年龄</td><td>{$age}</td></tr>
<tr><td>兵器</td><td>{$weapon}</td></tr>
</table>
<table>
<tr><td>姓名</td><td>{$zF.name}</td></tr>
<tr><td>年龄</td><td>{$zF.age}</td></tr>
<tr><td>兵器</td><td>{$zF.weapon}</td></tr>
</table>
<table>
<tr><td>姓名</td><td>{$gy[0]}</td></tr>
<tr><td>年龄</td><td>{$gy[1]}</td></tr>
<tr><td>兵器</td><td>{$gy['weapon']}</td></tr>
</table>
</body>
</html>
总结:
smarty可以赋值,数字等,也可以赋给一个数组,在模板解析数组是,用{$标签.key}
,或者{$标签[index]}
当键为字符串时:即索引数组时,用$标签[index]
,或$标签.key
.