+ -
当前位置:首页 → 问答吧 → 自用模板类!请老鸟拍砖

自用模板类!请老鸟拍砖

时间:2010-10-09

来源:互联网

复制代码
  1. if(!defined('ROOT')) exit();
  2. class Template{
  3.     private $leftTag="{";
  4.     private $rightTag="}";
  5.     private $cacheTime=0;
  6.     private $cache_dir="";
  7.     private $tpl_dir="";
  8.     private $tpl_c_dir="";
  9.     private $tVar=array();
  10.     public function __construct(){
  11.         $this->cache_dir=ROOT."cache/";
  12.         $this->tpl_dir=ROOT."tpl/";
  13.         $this->tpl_c_dir=ROOT."tpl_c/";
  14.         if(defined('L_Tag')){
  15.             $this->leftTag=L_Tag;
  16.         }
  17.         if(defined('R_Tag')){
  18.             $this->reftTag=R_Tag;
  19.         }
  20.         if(defined('Cache_Dir')&&is_dir(Cache_Dir)){
  21.             $this->cache_dir=preg_match("#/$#i",Cache_Dir)?Cache_Dir:Cache_Dir."/";
  22.         }
  23.         if(defined('Tpl_Dir')&&is_dir(Tpl_Dir)){
  24.             $this->tpl_dir=preg_match("#/$#i",Tpl_Dir)?Tpl_Dir:Tpl_Dir."/";
  25.         }
  26.         if(defined('Tpl_C_Dir')&&is_dir(Tpl_C_Dir)){
  27.             $this->tpl_c_dir=preg_match("#/$#i",Tpl_C_Dir)?Tpl_C_Dir:Tpl_C_Dir."/";
  28.         }
  29.     }
  30.     public function assign($var,$val=''){
  31.         if(is_array($var)){
  32.             $this->tVar=array_merge($this->tVar,$var);
  33.         }else if(is_object($var)){
  34.             foreach($var as $k=>$v){
  35.                 $this->tVar[$k]=$v;
  36.             }
  37.         }else if($var!=''){
  38.             $this->tVar[$var]=$val;
  39.         }
  40.     }
  41.     public function display($tpl){
  42.         if($tpl=="" || $tpl==null){
  43.             exit();
  44.         }
  45.         $source=$this->tpl_dir.$tpl;
  46.         $to=$this->tpl_c_dir.md5($tpl).".tpl.php";
  47.         //if(!file_exists($to) || filemtime($to)<filemtime($source)){    
  48.             $this->parse($source,$to);
  49.         //}
  50.         extract($this->tVar,EXTR_OVERWRITE);
  51.         include "$to";
  52.         return;
  53.     }
  54.     private function parse($s,$t){
  55.         if(!file_exists($s)){
  56.             die("模板 $s 不存在");
  57.         }
  58.         $content=file_get_contents($s);
  59.         $l=$this->leftTag;
  60.         $r=$this->rightTag;
  61.         //{load 'inc/head.html'}
  62.         $content=preg_replace("#{$l}load\s+'([^']+)'$r#ei","\$this->loadTemplate('\\1')",$content);
  63.         //{if($title==2)}
  64.         $content=preg_replace("#$l(if\s*\([^\)\r\n]+\))$r#i",'<?php \\1{ ?>',$content);
  65.         //{foreach($title as $k=>$v)}
  66.         $content=preg_replace("#{$l}foreach\s*\(([^\)\r\n]+)\)$r#i",'<?php foreach((array)\\1){ ?>',$content);
  67.         //{for($i=1;$i<10;$i++)}
  68.         $content=preg_replace("#$l(for\([^\)\r\n]+\))$r#i",'<?php \\1{ ?>',$content);
  69.         //{else}、{end}
  70.         $content=preg_replace("#{$l}else$r#i","<?php } else { ?>",$content);
  71.         $content=preg_replace("#{$l}end$r#i","<?php } ?>",$content);
  72.         //{include 'head.php'}
  73.         $content=preg_replace("#{$l}include\s+'([^']+)'$r#","<?php include '\\1'; ?>",$content);
  74.         //变量
  75.         $content=preg_replace("#$l(\\$[^{$r}\r\n]+)$r#ei","\$this->parseVar('\\1')",$content);
  76.         $content=preg_replace("#[\r\n]{2,}#i","\r\n",$content);
  77.         $content=preg_replace("#<!--[^>]+>#i","",$content);
  78.         //检查字段
  79.         $content="<?php if(!defined('ROOT')) exit();/* 编译文件创建于".date('Y-m-d')." */?>\r\n".$content;
  80.         $f=fopen($t,"w+");
  81.         fwrite($f,$content);
  82.         fclose($f);
  83.     }
  84.     function loadTemplate($file){
  85.         return file_get_contents($this->tpl_dir.(preg_match("#^/#i",$file)?substr($file,1):$file));
  86.     }
  87.     private function parseVar($str){
  88.         if(strpos($str,"|")===false){
  89.             return "<?php echo $str; ?>";
  90.         }
  91.         $arr=explode("|",$str);
  92.         $var=array_shift($arr);
  93.         foreach($arr as $v){
  94.             switch(strtolower($v)){
  95.                 case 'strip':
  96.                     $var="clearHtml($var)";break;
  97.                 case 'htmlencode':
  98.                     $var="htmlencode($var)";break;
  99.                 case 'htmldecode':
  100.                     $var="htmldecode($var)";break;
  101.                 //还可增加函数
  102.             }
  103.             if(preg_match("#[0-9]+#i",$v)){
  104.                 $var="getSub($var,$v)";
  105.             }else if(strpos(strtolower($v),"date(")!==false){
  106.                 $var=str_replace(")",",strtotime($var))",$v);
  107.             }
  108.         }
  109.         return "<?php echo $var; ?>";
  110.     }
  111. }

还有缓存没有实现。。。

作者: ynwcel   发布时间: 2010-10-09

只有一百来行代码,不错

作者: php100.org   发布时间: 2010-10-09