php模板技术原理的理解

<?php
/***************
* cls.php *
***************/
//cls.php -- 定义模板类
class lly_template
{
//设置一个叫template的模板
var $template_file_name;
var $content;
function lly_template($filename = "template.html")
{
//构建函数/template_file_name=$filename;
$this -> template_file_name = $filename;
}

function load()
{
//类被构建这后就执行load()函数
$fs = fopen($this -> template_file_name,"r");
//以读的方式把文件打开
$content_ = fread($fs, filesize($this -> template_file_name));
//把文件的所有字符赋值给$content
fclose($fs);//关闭fclose
$this -> content = $content_;//把字符赋值给$this->content
}
function repl($html_rep, $php_rep)
{
//这个是对象执行的第二不$html=subject;$php_rep="主题";
$this -> content = ereg_replace("\{".$html_rep."\}",$php_rep,$this -> content);//
}
}//这里面整个整个是一个封装的模板类
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>{subject}</title>
</head>

<body>
<p>{subject}</p>
<p>{content} </p>
</body>
</html>

这个模板类初始化是直接读出 templete.html 所有字符

<?php
/***************
* test.php *
***************/
//test.php -- 调用模板类
include("cls.php");
$html = new lly_template();//这里是真正调用一个模板类
$html -> load();
$html -> repl("subject","主题");
$html -> repl("content","内容");
echo $html -> content;
?>//

然后通过test.php对模板里的{}进行php执行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值