收藏本页 | 网站地图 | 投稿指南
 
 
当前位置:首页 >> 学院首页 >> 程序开发 >> PHP >>

A Sample Template Class For PHP

放大字体  缩小字体  At: 2006-05-23 21:36  By: master8 转载 来源: 互联网 作者: 年华
/**
* Nianhua.L PHP Template Class
*
* @author 年华 Email/MSN nianhua.liu@gmail.com QQ:4908220
* @link master8.net/blog || master8.net
* @version 1.0
* @copyright 2006 Master8.NET
* @package Nianhua.L PHPTC
*/


class template {
   /**
    * 模板文件存放目录[Template file dir]
    *
    * @var string
    */  
 var $tpl_default_dir;
   /**
    * 模板刷新时间[Template refresh time]
    *
    * @var int
    */  
 var $tpl_refresh_time;

   /**
    * 返回编译后的模板文件[Return compiled file]
    *
    * @return string
    */
 function tpl($file){
   $tplfile=$this->tpl_default_dir."/".$file.".htm";
   $compiledtpldir=$this->tpl_default_dir.".tpl";//构造编译目录[Define compile dir]
   $compiledtplfile=$compiledtpldir."/".$file.".tpl.php";//构造编译文件[Define compile file]
   is_dir($compiledtpldir) or @mkdir($compiledtpldir,0777);
   if(!file_exists($compiledtplfile) || (time()-@filemtime($compiledtplfile) > $this->tpl_refresh_time))//文件不存在或者创建日期超出刷新时间
   {
     $this->tpl_compile($tplfile,$compiledtplfile);//编译模板[Compile template]
   }
   return $compiledtplfile;
 }
   /**
    * 编译模板文件[Compile template]
    *
    * @return boolean
    */
 function tpl_compile($tplfile,$compiledtplfile){
   $str=$this->tpl_read($tplfile);
   $str=$this->tpl_parse($str,$language);
   if($this->tpl_write($compiledtplfile,$str))
   {
     return true;
   }
   return false;      
 }
   /**
    * 解析模板文件[Parse template]
    *
    * @return string
    */
 function tpl_parse($str){
   $str=preg_replace("/([\n\r]+)\t+/s","\\1",$str);
   $str=preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}",$str);
   $str=preg_replace("/\{template\s+(.+)\}/","\n\n",$str);
   $str=preg_replace("/\{include\s+(.+)\}/","\n\n",$str);
   $str=preg_replace("/\{if\s+(.+?)\}/","",$str);
   $str=preg_replace("/\{else\}/","",$str);
   $str=preg_replace("/\{elseif\s+(.+?)\}/","",$str);
   $str=preg_replace("/\{\/if\}/","",$str);
   $str=preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/","",$str);
   $str=preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/","\n \\3) { ?>",$str);
   $str=preg_replace("/\{\/loop\}/","\n\n",$str);
   $str=preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\((.+)\))\}/","",$str);
   $str=preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\((.+)\))\}/","",$str);
   $str=preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","",$str);
   $str=preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/s", "",$str);
   $str=preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s", "",$str);
   $str="\n".$str;//防止直接浏览模板编译文件
   return $str;
 }
   /**
    * 读取模板源文件[Read resource file]
    *
    * @return string
    */
 function tpl_read($tplfile){
   if($fp=@fopen($tplfile,"r") or die('Can not open tpl file'))
   {
     $str=fread($fp,filesize($tplfile));
     fclose($fp);
     return $str;  
   }
   return false;
 }
   /**
    * 写入模板编译文件[Write compiled file]
    *
    * @return boolean
    */
 function tpl_write($compiledtplfile,$str){
   if($fp=@fopen($compiledtplfile,"w") or die('Can not open tpl file'))
   {
     flock($fp, 3);
     if(@fwrite($fp,$str) or die('Can not write tpl file'))
     {
       fclose($fp);
       return true;
     }
     fclose($fp);
   }
   return false;
 }

}
?>


How to use?:http://master8.net/blog/attachment/how to use.txt

Demo:http://master8.net/blog/attachment/tpl.zip


 






         









 
Google
论坛精华  
 
 
  ©2005-2008 站长吧 Master8.NET All Rights Reserved 陕ICP备05010609号