在smarty模板中使用的变量有两种:一种是从php中分配的变量,另一种是从配置文件中读取的变量,需要注意的是模板中只能输出从php文件中分配的变量,不能在模板中为这些变量重新赋值。
1、 smarty中使用变量
1) 模板中的注释
模板注释被*号包围,例如 <{* this is a comment *}>
2)从php文件中分配过来的变量
连接数据库获取的变量
经过各种运算得到的结果
类型:
标量:string、int、float(double)、boolean
复合:array、object、null
关联数组:使用.连接下标;
索引数组:使用[]连接下标;
自定义数组
对象
对象名->属性
对象名->方法()
PHP代码层
//前面代码省略...
$smarty=new $smarty();
$head="这是我第一个网页";
$body="今天是我第一次使用smarty";
$smarty->assign("head",$head);
$smarty->assign("body",$body);
$smarty->display("test.tpl");
模板显示层
<html>
<head>
<{$head}> //使用<{$这个是你在smarty->assign中设置的变量}></head>
<body>
<{$body}> //使用<{$这个是你在smarty->assign中设置的变量}>
</body>
</html>