/* @author: [email protected] */
class Template{
var $code;
function Template($template){
$this->code = implode('', @file($template));
}
function assign($name,$var=null){
if(is_string($name) && is_string($var)){
$this->code = str_replace('{'.$name.'}', $var, $this->code);
} else if(is_array($var)){
list($this->code,$tmp,$end)=explode('',$this->code);
while(list(,$v)=each($var)){
$t=$tmp;$k2=$v2='';
while(list($k2, $v2) = each($v)){
$t = str_replace('{'.$k2.'}', $v2,$t);
}
$this->code .= $t;
}
$this->code .= $end;
} else {
while (list ($k2, $v2) = each($name)){
$this->code = str_replace('{'.$k2.'}', $v2, $this->code);
}
}
}
function display(){
echo $this->code;
}
}
?>
最简单的hello_world
准备一个php模版文件hello_world.html
模版中使用数组
test_array.html
User Name | |
{name} | {email} |
$users=array(
array('name'=>'jack','email'=>'[email protected]'),
array('name'=>'tom','email'=>'[email protected]')
);
$tpl->assign('users',$users);
$tpl->display();
?>
模版包含测试
{block}