模板引擎类

来源:互联网 发布:mysql 计划任务 编辑:程序博客网 时间:2024/06/02 02:47

模板引擎类

在html中穿插php代码,但是文件后缀名是php,为了让前端和后端的代码分离,后端就是.php用来获取数据的,前端就是.html用来展示数据的

preg_replace:将正则匹配到的字符串转换为指定的字符串

preg_replace_callback:

preg_quote:定界符 原子 元字符 模式修正符

Tpl.php代码:

<?phpclass Tpl{//模板文件的路径protected $viewDir='./view';//生成的缓存文件的路径protected $cacheDir='./cache';//过期时间protected $lifeTime=3600;protected $vars=[];//用来存放显示变量的数组function __construct($viewDir=null,$cacheDir=null,$lifeTime=null){if(!empty($viewDir)){if($this->checkDir($viewDir)){//判断所传路径是否是目录$this->viewDir=$viewDir;}}if(!empty($cacheDir)){if($this->checkDir($cacheDir)){$this->cacheDir=$cacheDir;}}if(!empty($liftTime)){$this->lifeTime=$lifeTime;}}protected function checkDir($dirPath){//如果目录不存在或者不是目录那面创建该目录if(!file_exists($dirPath)||!is_dir($dirPath)){return mkdir($dirPath,0755,true);}if(!is_writable($dirPat)||!is_readable($dirPath)){return chmod($dirPath,0755);}}//需要对外公开的方法//分配变量方法function assign($name,$value){$this->vars[$name]=$value;}//展示缓存文件方法$viewName模板文件名,模板文交是仅仅需要编译,还是先编译再包含进来,index.php?page=1为了让缓存的文件名不重复,将文件名和uri拼接起来在md5一下,生成缓存的文件名function display($viewName,$isInclude=true,$uri=null){//拼接模板文件的全路径$viewPath=rtrim($this->viewDir,'/').'/'.$viewName;if(!file_exists($viewPath)){die('模板文件不存在');}//拼接缓存文件的全路径$cacheName=md5($viewName.$uri).'.php';$cachePath=rtrim($this->cacheDir,'/').'/'.$cacheName;//根据缓存文件全路径判断缓存文件是否存在if(!file_exists($cachePath)){$php=$this->copile($viewPath);//编译模板文件file_put_contents($cachePath,$php);//写入文件,生成缓存文件}else{$isTimeout=(filectime($cachePath)+$this->lifeTime>time())?false:true;$isChange=filemtime($viewPath)>filemtime($cachePath)?true:false;if($isTimeout||$isChange){$php=$this->compile($viewPath);file_put_contents($cachePath,$php);}}//如果缓存文件不存在,编译模板文件,生成缓存文件//如果缓存文件存在,第一个判断缓存文件是否过期,第二个判断模板文件是否被修改,如果模板文件被修改过缓存文件需要重新生成//判断缓存文件是否需要包含进来if($isInclude){extract($this->vars);//展示缓存文件include $cachePath;}}protected function compile($filePath){//读取文件内容$html=file_get_contents($filePath);//正则替换$array=['{%%}'=>'<?=$\1;?>','{foreach%%}'=>'<?php foreach(\1):?>','{/foreach}'=>'<?php endforeach?>','{include %%}'=>'','{if %%}'=>'<?php if(\1):?>',];//遍历数组%%全部修改为.+,然后执行正则替换foreach($array as $key=>$value){//生成正则表达式$pattern='#'.str_replace('%%','(.+?)',preg_quote($key,'#')).'#';//实现正则替换if(strstr($pattern,'include')){$html=preg_replace_callback($pattern,[$this,'parseInclude'],$html);}else{//执行替换$html=preg_replace($pattern,$value,$html);}}return $html;//返回替换后的内容}protected function parseInclude($data){//处理include正则表达式,$data就是匹配到的内容//将文件名两边的引号去掉$fileName=trim($data[1],'\'"');//然后不包含文件生成缓存$this->display($fileName,false);//拼接缓存文件全路径$cacheName=md5($fileName).'php';$cachePath=rtrim($this->cacheDir,'/').'/'.$cacheName;return '<?php include "'.$cachePath.'"?>';}}?>
test.php

<?phpinclude "Tpl.php";$tpl=new Tpl();$title='你好';$data=['科比','韦德'];$tpl->assign('title',$title);$tpl->assign('data',$data);$tpl->display('test.html');?>
test.html
<html><head><title>{$title}</title></head><body>{foreach $data as $value}{$value}<br/>{/foreach}</body></html>



原创粉丝点击