学习笔记模板引擎类

<?php
 class TPL{
     protected $viewDir="./view/";
     protected $cacheDir="./cache/";
     protected $lifeTime=3600;
     protected $vars=[];
     public function __construct($viewDir=null,$cahceDir=null,$lifeTime=null){
         if(!empty($viewDir)){
             $this->checkDir($viewDir);//检测路径
             $this->viewDir=$viewDir;
         }
         if(!empty($cahceDir)){
             $this->checkDir($cahceDir);//检测路径
             $this->cahceDir=$cacheDir;
         }
         if(!empty($lifeTime)){
             $this->lifeTime=$lifeTime;
         }
     }
     protected function checkDir($filePath){//检测路径
         if(!(file_exists($filePath) && is_dir($filePath) )){
             mkdir($filePath);
         }
         if(!(is_readable($filePath) && is_writable($filePath) )){
             chmod($filePath, 755);
         }
     }
     public function assign($name,$value){//赋值
         $this->vars[$name]=$value;
     }
     public function display($viewName,$isInclude=true,$uri=null){//展示文件
         $viewPath=rtrim($this->viewDir,"/")."/".$viewName;//模板路径
         if(!file_exists($viewPath)){
             die("模板不存在");
         }
         $caheName=md5($viewName.$uri).".php";//缓存文件名
         $cachePath=rtrim($this->cacheDir,"/")."/".$caheName;//缓存路径
        if(!file_exists($cachePath)){//判断是否已经生成缓存文件,否,则生成缓存文件
             $php=$this->compile($viewPath);//编译模板文件
             file_put_contents($cachePath, $php);//生成缓存文件
         }
         else{//是,判断是否过期、被修改过
             $isTimeOut=(filectime($cachePath)+$this->lifeTime>time())?true:false;//判断是否过期
             $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){
         //将文件名两边的引号去掉
         $fileName=rtrim($data[1],"""");
         //然后不包含文件生成缓存
         $this->display($fileName,false);
         //拼接缓存文件全路径
         $cacheName=md5($fileName).".php";
         $cachePath=rtrim($this->cacheDir,"/")."/".$cacheName;
         return "<?php include "".$cachePath.""?>";
     }
 }
测试文件html:
<?php
 include "TPL.php";
 $tpl=new TPL();
 $title="测试";
 $data=["测试内容111","测试内容222"];
 $inc="include文件";
 $tpl->assign("title",$title);
 $tpl->assign("data",$data);
 $tpl->assign("inc",$inc);
 $tpl->display("test.html");
  
以上是 学习笔记模板引擎类 的全部内容, 来源链接: utcz.com/z/512449.html





