smarty引擎不仅在php程序的逻辑层需要使用,在表现层也会用到smarty语法,但并不是单纯的一对特殊的定界符中声明一个变量。
1)模板中的注释:
smarty默认定界符是{},因此注释为{**}当你修改定界符后,对其进行更改。
2)模板中的变量声明:
在smarty中,一切以变量为主,所有的呈现逻辑都让模板自行控制。
{$Name}{*常规类型的变量,需要调用模板中的assgin函数分配值*}
{$contact[roe].phone}{*数组类型变量*}
<body bgcolor="{#bgcolor#}">{*从配置文件中读取变量的值并输出*}
3)在模板中输出从php分配的变量
/*
index.php
*/
<?phprequire("./init.ini.php");
$title="smarty 的标题";
$content="1.1";
$tpl->assign("title",$title);
$tpl->assign("content",$content);
//从数据库db2表中获取
//连接数据库
//若未开启。。。php.ini开启。。。。
//mysqli;连接数据库
$mysqli=new mysqli("localhost","root","123","db2");
//insert delete update :不返回值,只返回影响的行数
//select :返回结果集
//结果集对象,穿件对象不能new mysqli_result,//而是通过调用mysqli的query方法直接返回结果集
$result = $mysqli->query("select * from mysql_user");
////第三步:使用该对象获取结果集中的数组
//mysql_fetch_array(assoc row)
//$result->fetch_assoc(row)
//索引数组可以一次性分配
$row=$result->fetch_row();
print_r($row);
$tpl->assign("result",$row);
//自定义数组
$tpl->assign("array1",array("1","2","3"));
$tpl->assign("array2",array(array("a","b"),array("c","d")));
$tpl->assign("array3",array("one"=>"one","two"=>"two"));
$tpl->assign("array4",array("one"=>array("aaaa"),array("two"=>"b")));
//$tpl->assign("name",["name"])
//对象的分配
class Person{
var $name;
var $age;
//第一种表示构造方法
public function __construct($name,$age){
$this->name=$name;
$this->age=$age;
}
/*第二种表示构造方法
function Person(){}*/
function say(){
return $this->name."的年龄是".$this->age;
}
}
//分配变量