PHP中面向对象中的模板引擎类

   & lt; php ?/* *   *,模版引擎类   */class  Tpl   {//缓存目录   protected  cacheDir 美元;=?缓存/';//模版目录   protected  tplDir 美元;='。Tpl/';//保存变量的成员方法   protected  vars 美元;=,[];//缓存有效期=protected  cacheLifeTime 美元;3600;//初始化成员属性   public  function  __construct (tplDir 美元;=,null , cacheDir 美元;=,null , cacheLifeTime 美元;=,null)   {//判断缓存目录是否存在   if (收取(tplDir美元)),{   if  ($ this→_checkDir (tplDir美元)),{=$ this→tplDir 空白(tplDir美元,“/?,只'/';   }   }//判断模板路径是否存在,,如果不存在要创建,权限是否可写,就需要更改权限   if (收取(cacheDir美元)),{   if  ($ this→_checkDir (cacheDir美元)),{=$ this→tplDir 空白(cacheDir美元,“/?,只'/';   }   }//初始化缓存时间   if (收取(cacheLifeTime美元)),{   时间=美元thsi→cacheLifeTime  cacheLifeTime美元;   }   }//目录的创建以及权限的处理   private  function  _checkDir(路径)   {//判断是否存在目录以及是否是目录   if  (! file_exists(路径)美元,| |,! is_dir(路径)美元),{   ,return  mkdir(路径,0755美元,真);   }//判断目录的权限是否可读可写   if  (! is_readable(路径),| |,! is_writable(路径)美元),{   return  chmod(路径,0755美元);   }      return 真实;   }//分配变量,主要的目的是将模板中需要处理的代码在php中替换做准备   public  function 分配(键,val美元)   {   $ this→var ($ key),=, val美元;      }//display 显示模板文件   public  function 显示器(文件,isExecute 美元=,true , uri 美元;=,null)   {      ,,//获得模板路径(模板目录路径+模板文件)   ,,$ tplFilePath =, $ this→tplDir。$文件;//判断文件是否存在   if  (! file_exists (tplFilePath美元)),{   退出('模板文件不存在的);}//获得编译文件路径(组合一个全新的文件名和路径)   $ cacheFile =, md5(文件。美元uri),只“。php”;      美元cacheFilePath ,=, $ this→cacheDir 只cacheFile美元;//检测是否编译过,是否修改过模板文件,如果变编译过,而且在有效期内则不编译   if  (! file_exists (cacheFilePath美元)),{   $ html =, $ this→编译($ tplFilePath);      },{else //如果修改过模版文件,,就需要删除原来的编译文件重新编译   if  (filemtime (tplFilePath美元),祝辞,filemtime (cacheFilePath美元)),{//删除原来的文件   分离($ cacheFilePath);//重新编译   $ html =, $ this→编译($ tplFilePath);      }//文件创建时间+缓存有效期,祝辞,时间(),(当前时间),,没有过期,,否则过期   isTimeout 美元;=,(filemtime (tplFilePath美元),+,$ this→cacheLifeTime 祝辞,时间()),?,false :,真的;      if  (isTimeout美元),{   $ html =, $ this→编译($ tplFilePath);   }   }//编译   if (收取(html)美元),{   if (!写入(cacheFilePath美元,,html)美元),{   退出(“编译文件写入失败”);   }   }//执行   if  (isExecute美元),{   提取($ this→var);   include  cacheFilePath美元;   }   }//编译的方法,,将html的php代码替换成php代码来执行,并且生成一个缓存   protected  function 编译(tplFilePath美元)   {//将整个文件读入一个字符串   $ html =, file_get_contents ($ tplFilePath);//正则替换规则   $ keys =, (   “{if  % %}”,=祝辞,‘& lt; ? php 如果(\ 1):,?在的,   ,,,,,,,,,,,的其他{},=祝辞,‘& lt; ? php  else : ?在的,   ,,,,,,,,,,,“{else  if  % %}”,=祝辞,‘& lt; ? php  elseif(\ 1),:, ?在的,   ,,,,,,,,,,,,“{elseif  % %}”=祝辞,‘& lt; ? php  elseif(\ 1),:, ?在的,   ,,,,,,,,,,,的{/如果},=祝辞,‘& lt; ? php  endif; ?在的,   ,,,,,,,,,,,‘{$ % %}’,=祝辞,‘& lt;=\ 1美元;?在的,   ,,,,,,,,,,,,“{foreach  % %}”=祝辞,‘& lt; ? php  foreach(\ 1),: ?在的,   ,,,,,,,,,,,的{/foreach},=祝辞,‘& lt; ? php  endforeach; ?在的,   ,,,,,,,,,,,,“{for  % %}”=祝辞,‘& lt; ? php (\ 1): ?在的,   ,,,,,,,,,,,的{/}”,=祝辞,‘& lt; ? php  endfor; ?在的,   ,,,,,,,,,,,,“{while  % %}”=祝辞,‘& lt; ? php 而(\ 1):?在的,   ,,,,,,,,,,,的{/}时,=祝辞,‘& lt; ? php  endwhile; ?在的,   ,,,,,,,,,,,“{继续}”,=祝辞,‘& lt; ? php 继续;?在的,   ,,,,,,,,,,,“{打破}”,=祝辞,‘& lt; ? php 断裂;?在的,   ,,,,,,,,,,,‘{$ % %,=,$ % %}”,=祝辞,‘& lt; ? php  \ 1美元,=,\ 2美元;?在的,   ,,,,,,,,,,,‘{$ % % + +}’,=祝辞,‘& lt; ? php  \ 1 + +美元;?在的,   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

PHP中面向对象中的模板引擎类