源代码下载地址:深刻浅出之Smarty模板引擎工做机制
接下来根据如下的Smarty模板引擎原理流程图开发一个本身的模板引擎用于学习,以便加深理解。
Smarty模板引擎的原理,实际上是这么一个过程:
把模板文件编译成php文件,而后每次都去读取下模板的修改时间,没有修改就不编译。而后include这个“编译”后的PHP文件。
所谓编译也就是模板用正则替换成含PHP代码的过程。
实际上并不会每次请求都编译,因此性能尚可。php
模板文件和php程序文件通过模板引擎的编译后合成为一个文件,即编译后的文件。html
接下来,咱们根据该原理流程写一个简单的模板引擎。。。。。。程序员
先贴上核心代码:web
Smarty.class.php文件正则表达式
<?php
class Smarty{
public $template_dir;//模板目录
public $compile_dir;//编译目录
public $arr=array();//定义一个数组,用以存放assign中的第二个参数传过来的值
public function __construct($template_dir="../templates",$compile_dir="../templates_c"){
$this->template_dir=$template_dir;//模板目录
$this->compile_dir=$compile_dir; //编译目录
}
public function assign($content,$replacment=null){
if($content!=""){ //若是指定模板变量,才将要赋的值存储到数组中
$this->arr[$content]=$replacment;
}
}
public function display($page){
$tplFile=$this->template_dir."/".$page;//读取模板文件,注意:若是模板目录下还有子目录,记得要写完整,好比,$smarty->display('Default/index.tpl')
if(!file_exists($tplFile)){
return;
}
$comFile=$this->compile_dir."/"."com_".$page.".php";
$tplContent=$this->con_replace(file_get_contents($tplFile));//将smarty标签替换为php的标签
file_put_contents($comFile,$tplContent);
include $comFile;
}
public function con_replace($content){
$pattern=array(
'/<{\s*\$([a-zA-Z_][a-zA-Z_0-9]*)\s*}>/i'
);
$replacement=array(
'<?php echo $this->arr["${1}"] ?>'
);
return preg_replace($pattern,$replacement,$content);
}
}
?>
Smarty.class.php代码解释:数据库
public function __construct($template_dir="../templates",$compile_dir="../templates_c")数组
{ 浏览器
$this->template_dir=$template_dir;服务器
$this->compile_dir=$compile_dir;函数
}
默认状况下,Smarty模板引擎将把templates目录用于存放模板文件,templates_c用于存放编译后的文件
那为什么要$replacement的值保存到数组中呢?
其实内部操做是这么一个流程:将$replacement值保存到数组--->读取模板文件(index.dwt,由display函数完成)--->将数组中的值匹配给模板文件中的变量(由con_replace()函数完成)--->将替换后的模板文件写入到编译文件中(com_index.dwt.php)--->输出编译后的PHP文件
/*Smarty.ini.php文件:用于完成初始化smarty的工做*/
<?php
include "./libs/Smarty.class.php";
$tpl=new Smarty();
$tpl->template_dir="./Tpl";
$tpl->compile_dir="./Compile";
?>
<!--模板文件-->
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><{$title}></title>
</head>
<body>
<p>内容:<{$content}></p>
<p>做者:<{$auth}></p>
<p>网址:<{$website}></p>
</body>
</html>
/*index.php文件*/
<?php
include "./Smarty.ini.php";
$title="深刻浅出之Smarty模板引擎工做机制";
$content="Smarty模板引擎工做机制流程图";
$auth="MarcoFly";
$website="www.MarcoFly.com";
$tpl->assign("title",$title);
$tpl->assign("content",$content);
$tpl->assign("auth",$auth);
$tpl->assign("website",$website);
$tpl->display("index.dwt");
?>
该index.php就是PHP程序员编写的,能够从数据库中获取各类想要的数据,并保存到变量中,而后简单的调用assign()函数将数据保存到数组中,并经过display()函数将编译文件输出
注:此编译文件是php文件,经过服务器端执行,将结果输出的客户端的浏览器上
分析到这里,咱们回过头来分析下在深刻浅出之Smarty模板引擎工做机制(一)中给出的关于编译后的文件代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><?php echo $this->arr["title"] ?></title>
</head>
<body>
<p>内容:<?php echo $this->arr["content"] ?></p>
<p>做者:<?php echo $this->arr["auth"] ?></p>
<p>网址:<?php echo $this->arr["website"] ?></p>
</body>
</html>
因为咱们已经经过assign()函数,将要赋给模板标签中变量的值保存到了数组中了,即此时编译后的模板文件,能够直接输出该数组中的值了。
举个例子:
$title="深刻浅出之Smarty模板引擎工做机制";
$tpl->assign("title",$title);
当执行了以上两句代码后,在数组$arr中就存放着下标为:title,值为:深刻浅出之Smarty模板引擎工做机制的关联数组了。
此时,就能够直接经过$this->arr['title']直接输出该数组的值。
至于对如何从<{$title}> ---> <?php echo $this->arr['title']?> 的转换,不懂的读者能够再仔细看下con_replace()函数。
有了以上几个文件以后,咱们在浏览器中访问index.php文件将获得如下结果:
到此,咱们“开发”了一个本身的模板引擎,而且测试成功,固然,这只是供交流学习之用。若是你以为这篇文章对你了解smarty模板引擎的工做机制有所帮助的话,请帮忙顶一顶哈O(∩_∩)O~
文章出自:WEB开发_小飞
转载请注明出处:http://www.cnblogs.com/hongfei/archive/2011/12/10/Smarty-two.html