一个自己写的很简陋的php模板类
<?php/** * 模板类 ** 1、变量表示//{$name} 被解析成<?=$name?>,表示显示变量$name的值,其中的“name”由英文字母、数字和下划线组成首字母必须是英文//字母或者下划线。////2、常量表示//{name} 被解析成<?=name?>,表示显示常量name的值,其中的“name”由英文字母、数字和下划线组成首字母必须是英文字//母或者下划线。////3、函数调用//{$functionname(*)} 将被解析成 <?=functionname(*)?>////4、标签调用(方便编辑使用)//标签格式类似于 {tag_artile('fenlei'=>2,'row'=>10,'sub'=>20,'time'=>1)} ,此标签将被解析为对应的函数<?php echo //artile(array('fenlei'=>2,'row'=>10,'sub'=>20,'time'=>1));?>////5、条件判断//{if *} * {else} * {else} * {/if} 或者 {if *} * {/if},其中{if *}中的*就是此判断语句的条件表达式,符合php的表//达式。////6、循环//{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) { ?> * <? } ?>////7、模板嵌套//{template 'module name','file name'},例如:{template 'artile','header'} 表示嵌套 artile模块模板目录下 //header.html * */class templateClass{var $template_file ;//模板存放var $template_cache_file ;//编译过后的模板var $template_data ;//模板数据获取方式(文件/数据库)var $t_hx = '.html';//模板后续var $tc_hx = '.tpl.php';//编译过后模板的后续/** * 析构函数 * * @param string $template_file //模板存放路径 * @param string $template_cache_file //编译过后的模板存放路径 */function __construct($template_file= "template",$template_cache_file= "template_cache",$template_data='file'){$this->template_file = $template_file;$this->template_cache_file = $template_cache_file;$this->template_data = $template_data;}/** * 输出错误信息 * * @param string $msg */function error($msg){print "<div style=\"font-size:12px;color:red;\">".$msg."</div>";}/** * 编译模板(创建,更新用) * * @param string $module * @param string $template * @return string 失败返回0 成功返回编译过后的地址 */function template_compile($module,$template){try{if($this->template_data='file'){$content = file_get_contents($this->template_file.'/'.$module.'/'.$template.$this->t_hx);}else{//数据库方式获取}$content = $this->template_parse($content);$compiledtplfile = $this->template_cache_file.'/'.$module.'_'.$template.$this->tc_hx;$strlen = file_put_contents($compiledtplfile, $content);@chmod($compiledtplfile, 0777);return $compiledtplfile;}catch (Exception $e){return 0;}}/** * 获取Tpl编译过后的模板缓存文件 * * @param string $module * @param string $template */function template_getTpl($module,$template){$templateFile = $this->template_file.'/'.$module.'/'.$template.$this->t_hx;$compiledtplfile = $this->template_cache_file.'/'.$module.'_'.$template.$this->tc_hx;//如果tpl不存在 或者 模板文件的时间大于生成tpl文件的时间 就重新编译if(!file_exists($compiledtplfile) || filemtime($templateFile)>filemtime($compiledtplfile)){$this->template_compile($module,$template);}return $compiledtplfile;}/** * 更新整个模块里面的模板 * * @param unknown_type $module * @return unknown */function template_module($module){$files = glob($this->template_file.'/'.$module.'/*.html');if(is_array($files)){foreach($files as $tpl){$tpl = split('/',$tpl);$tpl = split('.html',$tpl);$tpl = $tpl;$this->template_compile($module, $tpl);}}return TRUE;}/** * 模板缓存 * * @return unknown */function template_cache(){foreach($MODULE as $module=>$m){template_module($module);}return TRUE;}/** * 模板解析 * * @param string $str * @return string */function template_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("/\{php\s+(.+)\}/","\n<?php \\1?>\n",$str);$str = preg_replace("/\{if\s+(.+?)\}/","<?php if(\\1) { ?>",$str);$str = preg_replace("/\{else\}/","<?php } else { ?>",$str);$str = preg_replace("/\{elseif\s+(.+?)\}/","<?php } elseif (\\1) { ?>",$str);$str = preg_replace("/\{\/if\}/","<?php } ?>",$str);$str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/","<?php if(is_array(\\1)) foreach(\\1 AS \\2) { ?>",$str);$str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/","\n<?php if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>",$str);$str = preg_replace("/\{\/loop\}/","\n<?php } ?>\n",$str);$str = preg_replace("/\{tag_([^}]+)\}/e", "get_tag('\\1')", $str);$str = preg_replace("/\{(*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str);$str = preg_replace("/\{\\$(*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str);$str = preg_replace("/\{(\\$*)\}/","<?php echo \\1;?>",$str);$str = preg_replace("/\{(\\$\'\"\$\x7f-\xff]+)\}/es", "addquote('<?php echo \\1;?>')",$str);$str = preg_replace("/\{(*)\}/s", "<?php echo \\1;?>",$str);return $str;}}//以下为外部函数,共模板调用/** * 将$var字符串,转换为可执行的php代码形式,并返回其结果.. * 转换为”,将为转换为['xxx'] * * @param unknown_type $var * @return unknown */function addquote($var){return str_replace("\\\"", "\"", preg_replace("/\[(+)\]/s", "['\\1']", $var));}/** * 获取标签tag * * @param string $tagname * @return unknown */function get_tag($tagname){if($tagname){//print_r(explode('(',$tagname));$tagname = str_replace('(','(array(',$tagname);$tagname = str_replace(')','))',$tagname);return "<?php echo {$tagname};?>";}}?>
页:
[1]