php Smarty模板引擎工作机制(二)

作者:袖梨 2022-06-25

所谓编译也就是模板用正则替换成含PHP代码的过程。
实际上并不会每次请求都编译,所以性能尚可。

模板文件和php程序文件经过模板引擎的编译后合成为一个文件,即编译后的文件。

接下来,我们根据该原理流程写一个简单的模板引擎。。。。。。

先贴上核心代码:

Smarty.class.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;
if(!file_exists($tplFile)){
return;
}
$comFile=$this->compile_dir."/"."com_".$page.".php";
$tplContent=$this->con_replace(file_get_contents($tplFile));
file_put_contents($comFile,$tplContent);
include $comFile;
}
public function con_replace($content){
$pattern=array(
'//i'
);
$replacement=array(
'arr["${1}"] ?>'
);
return preg_replace($pattern,$replacement,$content);
}
}
?>

Smarty.class.php代码解释:

$template_dir 指定模板文件的目录

$compile_dir 指定编译后的模板文件的目录

构造函数

代码如下 复制代码

public function __construct($template_dir="../templates",$compile_dir="../templates_c")

{

$this->template_dir=$template_dir;

$this->compile_dir=$compile_dir;

}

默认情况下,Smarty模板引擎将把templates目录用于存放模板文件,templates_c用于存放编译后的文件

assign($content,$replacment=null)函数的工作机制是将每次要传递给模板中的变量的值通过语句:

$this->arr[$content]=$replacment;保存到数组中。 

那为何要$replacement的值保存到数组中呢?

其实内部操作是这么一个流程:将$replacement值保存到数组--->读取模板文件(index.dwt)--->将数组中的值匹配给模板文件中的变量(由con_replace()函数完成)--->将替换后的模板文件写入到编译文件中(com_index.dwt.php)--->输出编译后的PHP文件

dispaly($page)函数接收一个参数,即要输出的模板文件(index.dwt) 首先,将模板文件的路径赋给$tplFile($tplFile=$this->template_dir."/".$page) 

判断模板文件是否存在,如果不存在,就没必要加载了,直接return

指定一个编译文件,以便存放替换变量后的模板文件

通过函数file_get_contents()读取模板文件,并通过函数conf_replace()替换掉模板中的smarty标签

将替换变量后的模板文件通过file_put_contents()写入到编译文件中

将编译后的文件include进来,即可输出编译后的文件

函数con_replace($content)用于替换模板文件(index.dwt)中的变量,即将php中的变量值赋给模板中的变量 通过一个可以匹配形式的正则表达式匹配模板文件中的内容,并将匹配到的值替换为的形式

匹配到内容,并将替换后的内容返回

代码如下 复制代码

/*Smarty.ini.php文件:用于完成初始化smarty的工作*/
include "./libs/Smarty.class.php";
$tpl=new Smarty();
$tpl->template_dir="./Tpl";
$tpl->compile_dir="./Compile";
?>









内容:


作者:


网址:



复制代码

代码如下 复制代码

/*index.php文件*/
include "./Smarty.ini.php";
$title="深入浅出之Smarty模板引擎工作机制";
$content="Smarty模板引擎工作机制流程图";
$auth="MarcoFly";
$website="";
$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文件,通过服务器端执行,将结果输出的客户端的浏览器上

相关文章

精彩推荐