复制代码
- if(!defined('ROOT')) exit();
- class Template{
- private $leftTag="{";
- private $rightTag="}";
- private $cacheTime=0;
- private $cache_dir="";
- private $tpl_dir="";
- private $tpl_c_dir="";
- private $tVar=array();
- public function __construct(){
- $this->cache_dir=ROOT."cache/";
- $this->tpl_dir=ROOT."tpl/";
- $this->tpl_c_dir=ROOT."tpl_c/";
- if(defined('L_Tag')){
- $this->leftTag=L_Tag;
- }
- if(defined('R_Tag')){
- $this->reftTag=R_Tag;
- }
- if(defined('Cache_Dir')&&is_dir(Cache_Dir)){
- $this->cache_dir=preg_match("#/$#i",Cache_Dir)?Cache_Dir:Cache_Dir."/";
- }
- if(defined('Tpl_Dir')&&is_dir(Tpl_Dir)){
- $this->tpl_dir=preg_match("#/$#i",Tpl_Dir)?Tpl_Dir:Tpl_Dir."/";
- }
- if(defined('Tpl_C_Dir')&&is_dir(Tpl_C_Dir)){
- $this->tpl_c_dir=preg_match("#/$#i",Tpl_C_Dir)?Tpl_C_Dir:Tpl_C_Dir."/";
- }
- }
- public function assign($var,$val=''){
- if(is_array($var)){
- $this->tVar=array_merge($this->tVar,$var);
- }else if(is_object($var)){
- foreach($var as $k=>$v){
- $this->tVar[$k]=$v;
- }
- }else if($var!=''){
- $this->tVar[$var]=$val;
- }
- }
- public function display($tpl){
- if($tpl=="" || $tpl==null){
- exit();
- }
- $source=$this->tpl_dir.$tpl;
- $to=$this->tpl_c_dir.md5($tpl).".tpl.php";
- //if(!file_exists($to) || filemtime($to)<filemtime($source)){
- $this->parse($source,$to);
- //}
- extract($this->tVar,EXTR_OVERWRITE);
- include "$to";
- return;
- }
- private function parse($s,$t){
- if(!file_exists($s)){
- die("模板 $s 不存在");
- }
- $content=file_get_contents($s);
- $l=$this->leftTag;
- $r=$this->rightTag;
- //{load 'inc/head.html'}
- $content=preg_replace("#{$l}load\s+'([^']+)'$r#ei","\$this->loadTemplate('\\1')",$content);
- //{if($title==2)}
- $content=preg_replace("#$l(if\s*\([^\)\r\n]+\))$r#i",'<?php \\1{ ?>',$content);
- //{foreach($title as $k=>$v)}
- $content=preg_replace("#{$l}foreach\s*\(([^\)\r\n]+)\)$r#i",'<?php foreach((array)\\1){ ?>',$content);
- //{for($i=1;$i<10;$i++)}
- $content=preg_replace("#$l(for\([^\)\r\n]+\))$r#i",'<?php \\1{ ?>',$content);
- //{else}、{end}
- $content=preg_replace("#{$l}else$r#i","<?php } else { ?>",$content);
- $content=preg_replace("#{$l}end$r#i","<?php } ?>",$content);
- //{include 'head.php'}
- $content=preg_replace("#{$l}include\s+'([^']+)'$r#","<?php include '\\1'; ?>",$content);
- //变量
- $content=preg_replace("#$l(\\$[^{$r}\r\n]+)$r#ei","\$this->parseVar('\\1')",$content);
- $content=preg_replace("#[\r\n]{2,}#i","\r\n",$content);
- $content=preg_replace("#<!--[^>]+>#i","",$content);
- //检查字段
- $content="<?php if(!defined('ROOT')) exit();/* 编译文件创建于".date('Y-m-d')." */?>\r\n".$content;
- $f=fopen($t,"w+");
- fwrite($f,$content);
- fclose($f);
- }
- function loadTemplate($file){
- return file_get_contents($this->tpl_dir.(preg_match("#^/#i",$file)?substr($file,1):$file));
- }
- private function parseVar($str){
- if(strpos($str,"|")===false){
- return "<?php echo $str; ?>";
- }
- $arr=explode("|",$str);
- $var=array_shift($arr);
- foreach($arr as $v){
- switch(strtolower($v)){
- case 'strip':
- $var="clearHtml($var)";break;
- case 'htmlencode':
- $var="htmlencode($var)";break;
- case 'htmldecode':
- $var="htmldecode($var)";break;
- //还可增加函数
- }
- if(preg_match("#[0-9]+#i",$v)){
- $var="getSub($var,$v)";
- }else if(strpos(strtolower($v),"date(")!==false){
- $var=str_replace(")",",strtotime($var))",$v);
- }
- }
- return "<?php echo $var; ?>";
- }
- }
|