最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php 模板写法
时间:2022-07-02 10:46:57 编辑:袖梨 来源:一聚教程网
YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.***w3.org/TR/REC-html40/loose.dtd">
YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.***w3.org/TR/REC-html40/loose.dtd">
/* @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
{title}
接下来就是模版的翻译工作了
include('../include/template.php'); //包含模版核心类文件
$tpl=new Template('hello_world.html'); //参数为模版路径和文件名,可以使用相对路径,也可以使用绝对路径
$tpl->assign('title',"hello world!"); //将标签{title} 替换成hello world
$tpl->display();
?>
模版中使用数组
test_array.html
{user} {email}
{user1} {email1}
模版处理文件
include('../include/template.php');
$tpl=new Template('test_array.html');
$user=array('user'=>'yubing','email'=>'[email protected]');
$tpl->assign($user);
$tpl->assign('user1','jack');
$tpl->assign('email1','[email protected]');
$tpl->display();
?>
简单的区块处理
block.html
User Name | |
{name} | {email} |
区块处理程序
block.php
include('../include/template.php');
$tpl=new Template('block.html');
$users=array(
array('name'=>'jack','email'=>'[email protected]'),
array('name'=>'tom','email'=>'[email protected]')
);
$tpl->assign('users',$users);
$tpl->display();
?>
模版包含测试
{title}
{block}
模版包含处理程序
include('../include/template.php');
$tpl=new Template('block.html');
$users=array(
array('name'=>'jack','email'=>'[email protected]'),
array('name'=>'tom','email'=>'[email protected]'),
);
$tpl->assign('users',$users);
$block=$tpl->code;
$tpl->Template('main.html');
$tpl->assign('block',$block);
$tpl->assign(array('title'=>'测试多模版文件'));
$tpl->display();
?>
相关文章
- 无主之地4卡卡地亚焚毁区传奇装备掉落一览 卡卡地亚焚毁区传奇装备位置及触发条件 09-18
- 伊瑟月塔30层莎朗打法教学 09-18
- 原神5星命座免费获取方法 09-18
- 零度曙光弓箭流派搭配推荐 09-18
- 王者荣耀亚瑟荣光之誓获取攻略 09-18
- 无限暖暖音乐季隐藏任务闪闪的愿望攻略一览 09-18