您当前的位置:学无止境 > 仿smarty简易模板引擎网站首页学无止境
仿smarty简易模板引擎
发布时间:2017-11-24 14:19:49编辑:三青查看次数:1446
<?php
class template{
//定义一个私有成员用来存储模板引擎源文件的所在目录
private $templateDir;
//这个私有成员用来存储编译之后文件的存放目录
private $compileDir;
//在模板文件中需要替换掉的那些变量,它们需要一个标记,让模板引擎去识别需要替换的是哪一部分
//在Smarty模板引擎中默认是一个{ 这里不跟Smarty一样
private $leftTag = '{#';
private $rightTag = '#}';
//这个私有成员用来存储当前正在编译的模板文件名
private $currentTemp = '';
//这个私有成员用来存放当前正在编译的模板文件中的html代码。
//也就是从源文件中读取一段html代码存放在$outputHtml中,然后通过一系列的正则替换,最后再将这段html代码写入到一个目标文件中,这个文件就是编译之后的模板文件。
private $outputHtml;
//这个是一个变量池,在编译模板源文件之前,会把模板中需要用到的变量,把它们的值通通存到这个变量池中。
//当模板文件被编译之后,就可以从这个变量池中根据标记获取到它们需要的值。
private $varPool = array();
//开发一个构造函数,定义几个参数,在实例化的时候,
public function __construct($templateDir, $compileDir, $leftTag = null, $rightTag = null){
//把前两个参数赋值给对应的私有成员
$this->templateDir = $templateDir;
$this->compileDir = $compileDir;
//第三,第四个参数回去检查一下它是否为空,如果为空,就使用默认的私有成员的值
if(!empty($leftTag)){
$this->leftTag = $leftTag;
}
if(!empty($rightTag)){
$this->rightTag = $rightTag;
}
}
//模板引擎类--写入和获取数据
//这个目标的作用是在编译模板之前把模板中需要用到的变量,全部放到变量池中,并且给定义一个标记
public function assign($tag, $var){
$this->varPool[$tag] = $var;
}
//获取数据的方法
public function getVar($tag){
return $this->varPool[$tag];
}
//模板引擎类--获取模板源文件;第1个参数(模板源文件的名称),第2个参数(默认的模板文件扩展名),也可以设置为私有成员
public function getSourceTemplate($templateName = null, $ext = '.html'){
$this->currentTemp = $templateName;
//把模板源文件的完整路径拼接出来
$sourceFilename = $this->templateDir.$this->currentTemp.$ext;
//把模板源文件中的html代码赋值给outputHtml私有成员
$this->outputHtml = file_get_contents($sourceFilename);
}
//模板引擎类--模板编译(一);通过$templateName去生成它的编译文件,默认为空
public function compileTemplacte($templateName = null, $ext = '.html'){
//先获取到当前处理的模板文件的名称,首先检测一下它是否为空,是则从私有成员中获取,否则就用用户输入的$templateName
$templateName = empty($templateName) ? $this->currentTemp : $templateName;
//核心代码,编译模板中需要用到的正则替换
//左侧的{#
$pattern = '/'.preg_quote($this->leftTag);
$pattern .= '\$([a-zA-Z]\w*)';
//右侧的#}\
$pattern .= preg_quote($this->rightTag).'/';
//需要对outputHtml,也就是从模板源文件中获取到的这段html代码中去寻找,用这个正则表达式去匹配这个标记,然后将它替换成我们想要的那种PHP代码,也就是PHP能够识别的那种变量的代码。
//使用preg_replace函数作一个正则的替换,$1表示使用这个$pattern匹配到的数据中的子模式,并且是第一个子模式
$this->outputHtml = preg_replace($pattern, '<?php echo $this->getVar(\'$1\'); ?>', $this->outputHtml);
//经过正则替换之后,这个outputHtml就是编译之后的html代码,将它生成一个目标文件
$compiledFilename = $this->compileDir.md5($templateName).$ext;
file_put_contents($compiledFilename, $this->outputHtml);
}
//模板引擎类--显示模板
public function display($templateName = null, $ext = '.html'){
//display()参数不填,默认源文件明为控制器文件名
if (empty($templateName)) {
$templateName = preg_replace('/^[\s\S]*\/([a-zA-Z_]\w*)\.\w+$/','$1',$_SERVER['PHP_SELF']);
}
$templateName = empty($templateName) ? $this->currentTemp : $templateName;
//检测编译目录下是否已经存在编译的目标文件,不存在则编译它
if(file_exists($this->compileDir.md5($templateName).$ext)){
// 获取源文件的最后修改时间
$getSourceTemplateTime = filemtime($this->templateDir.$templateName.$ext);
// 获去编译文件的最后编译时间
$getCompiledTemplateTime = filemtime($this->compileDir.md5($templateName).$ext);
//源文件是不是比编译后的文件修改时间更迟,如果是说明源文件已经被改动过了,需重新编译这个模板;反之,则说明编译后没有再修改过模板源文件,那么就直接加载已经编译好的模板源文件
if($getSourceTemplateTime>$getCompiledTemplateTime){
//获取到模板的源文件
$this->getSourceTemplate($templateName);
//编译模板文件,不传名称,使用默认的index
$this->compileTemplacte();
}
}else {
//获取到模板的源文件
$this->getSourceTemplate($templateName);
//编译模板文件,不传名称,使用默认的index
$this->compileTemplacte();
}
//将代码输出,用include_once来包含编译后的模板文件
include_once $this->compileDir.md5($templateName).$ext;
}
}
如有错误 欢迎留言指正,转载请注明来至http://lovesanqing.com/knowledgeslist.html
关键字词:smarty,模板引擎,核心文件,正则,php