做smarty一般上网下载一个模板我是下载了一个压缩包, 压缩包—解压--获取其中的---libs文件
写一个配置文件init.inc.php
<?php
require("./libs/Smarty.class.php");
$tpl=new Smarty;
$tpl->template_dir='./templates';
$tpl->compile_dir='./templates_c';
$tpl->left_delimiter='<{';
$tpl->right_delimiter='}>';
?>
写一个b.php
<?php
include("./init.inc.php");
$a=10;
$tpl->assign("title","this is meinv");
$tpl->assign("title2",$a);
$tpl->assign("title3",true);
//从数据库json,表smarty_user获取
//$mysqli=new mysqli();
//$mysqli->connect("localhost","root",123);
//mysqli->select_db("json");
$mysqli=new mysqli("localhost","root",123,"json");
//insert delete update:返回影响的行数
//select:返回结果集
//mysqli_result类 结果集类
//创建结果集对象不能new ,而是通过调用mysqli的query方法直接返回结果集对象
$result=$mysqli->query("select * from smarty_user");
//使用该对象获取结果集中的数组
//mysql_fetch_array(assoc row)
//$result->fetch_assoc(row)
$row=$result->fetch_assoc();
print_r($row);
//user表里字段包括(id,name,age,email)
$tpl->assign("id",$row['id']);
$tpl->assign("name",$row['name']);
$tpl->assign("age",$row['age']);
$tpl->assign("email",$row['email']);
$tpl->assign("result",$row);
$row=$result->fetch_row();
print_r($row);
$tpl->assign("row",$row);
//自定义数组
$tpl->assign("array1",array("1","2","3"));
$tpl->assign("array2",array(array("a","b"),array("c","d")));
$tpl->assign("array3",array("a"=>"b"));
$tpl->assign("array4",array("a"=>array("b"),array("two"=>"bb")));
class person{
var $name;
var $age;
public function __construct($name,$age){
$this->name=$name;
$this->age=$age;
}
function say(){
return $this->name."的年龄是".$this->age;
}
}
//分配变量
$tpl->assign("person",new person("zs",20));
//运算
$tpl->assign("num1",10);
$tpl->assign("num2",20);
$tpl->display("a.html");
?>
写一个a.html获取b.php中的代码
模板文件中的变量分类<br />
一、由php中分配过来的变量<br />
<br />
<{*这是smarty的注释信息*}>
<!-- 这是html注释 -->
这是从php分配过来的标量类型的变量
<{$title}><br />
<{$title2}><br />
<{$title3}><br /><br />
<{$id}><br />
<{$name}><br />
<{$age}><br />
<{$email}><br />
这是关联数组<br />
<{$result['id']}><br />
<{$result['name']}><br />
<{$result['age']}>
<br /><br /><br />
<{$row[0]}><br />
<{$row[1]}><br />
<{$row[2]}><br />
自定义
<{$array1[0]}><br />
<{$array1[1]}><br />
<{$array1[2]}><br />
<{$array2[0][0]}><br />
<{$array2[0][1]}><br />
<{$array2[1][0]}><br /><br /><br />
guanliangshuzu xianshi
<{$array3['a']}><br />
<{$array4.a[0]}><br /><br />
<{$person->say()}>
<{$person->name}>
<{$person->age}><br /><br />
<{$num1+$num2}><br />
<{$num1+$num2*2}><br />