Discuz模板类的点滴说明

 
template.php [php] <?php /** * Nianhua.L PHP Template Class * * @author 年华 <Email/MSN [email]nianhua.liu@gmail.com[/email]> <QQ:30773700> * @link 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]                 }                 clearstatcache();                 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<?php include template(\\1); ?>\n",$str);                 $str=preg_replace("/\{include\s+(.+)\}/","\n<?php include \\1; ?>\n",$str);                 $str=preg_replace("/\{if\s+(.+?)\}/","<? if(\\1) { ?>",$str);                 $str=preg_replace("/\{else\}/","<? } else { ?>",$str);                 $str=preg_replace("/\{elseif\s+(.+?)\}/","<? } elseif (\\1) { ?>",$str);                 $str=preg_replace("/\{\/if\}/","<? } ?>",$str);                 $str=preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/","<? if(is_array(\\1)) foreach(\\1 AS \\2) { ?>",$str);                 $str=preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/","\n<? if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>",$str);                 $str=preg_replace("/\{\/loop\}/","\n<? } ?>\n",$str);                 $str=preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\((.+)\))\}/","<?=\\1?>",$str);                 $str=preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\((.+)\))\}/","<?=\\1?>",$str);                 $str=preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","<?=\\1?>",$str);                 $str=preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/s", "<?=\\1?>",$str);                 $str=preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s", "<?=\\1?>",$str);                 $str="<? if(!defined('IN')) exit('Access Denied'); ?>\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;         } } ?> [/php] 使用举例 在程序目录下建templates/default文件夹,里面随便写个模板文件index.html,内容为{$index} 在程序目录下建index.php,内容以下 [php] <?php define('IN', TRUE); //定义常量,防止直接浏览模板编译文件 $rootpath = dirname(__FILE__); //获取站点根目录 $tpl_refresh_time = 120;// 模板刷新(以秒为单位)时间,0为不缓存 $tpl_default_dir = $rootpath.'/templates/default';//默认模板存放目录[推荐分别定义文件夹,而后组合] require $rootpath.'/template.php'; $t = new template; $t->tpl_default_dir = $tpl_default_dir; $t->tpl_refresh_time = $tpl_refresh_time; $index = 'This is index'; include $t->tpl('index'); ?> [/php] 模板解析的规则    一、变量表示       {$name}被解析成<?=$name?>,表示显示变量$name的值,其中的“name”由英文字母、数字和下划线组成首字母必须是英文字母或者下划线。    二、常量表示       {name}被解析成<?=name?>,表示显示常量name的值,其中的“name”由英文字母、数字和下划线组成首字母必须是英文字母或者下划线。    三、条件判断       {if *} * {else} * {elseif} * {/if} 或者 {if *} * {/if},其中{if *}中的*就是此判断语句的条件表达式,符合php的表达式。    四、循环       {loop $a $b} * {/loop} 或者 {loop $a $b $c} * {/loop} ,{loop $a $b} * {/loop}被解析成<? if(is_array($a)) foreach($a AS $b) { ?> * <? } ?> ,而{loop $a $b $c} * {/loop}则被解析成 <? if(is_array($a)) foreach($a AS $b=>$c) { ?> * <? } ?>    五、函数       {$fun($a,$b)}被解析成<? fun($a,$b); ?> 呵呵,只是完成了基本功能,能够简单使用。 第一次在这里发东西,你们多提意见...
相关文章
相关标签/搜索