zshtom
中级会员
 
初级会员
UID 66877
精华
0
积分 240
帖子 299
金钱 240 喜悦币
威望 0
人脉 0
阅读权限 30
注册 2005-9-23
状态 离线
|
没事玩写的30行的模板功能很简单
<?php
/**
* @author zshtom
* @copyright zshtom 0304681@126.com 2007-2008
*/
function zTemplate($filename, $templatedir='', $compliedir=''){
$comtime = @filemtime($compliedir.md5($filename).".php");//编译时间
$temtime = @filemtime($templatedir.$filename);//模板时间
if($temtime > $comtime){//是否重新写入模板
$str = file_get_contents($templatedir.$filename);
$str = preg_replace('|<!--{if (.+)}-->|isU', '<?php if(\\1){ ?>', $str);
$str = preg_replace('|<!--{/if}-->|isU', '<?php } ?>', $str);
$str = preg_replace('|{\$(.*)}|isU', '<?php echo $\\1; ?>', $str);
$str = preg_replace('|<!--{else}-->|isU', '<?php }else{ ?>', $str);
$str = preg_replace('|<!--{elseif (.*)}-->|isU', '<?php }elseif(\\1){ ?>', $str);
$str = preg_replace('|{eval (.*)}|isU', '<?php \\1; ?>', $str);
$str = preg_replace('|{echo (.*)}|isU', '<?php echo \\1; ?>', $str);
$str = preg_replace('|{include_php (.*)}|isU', '<?php if(is_file("\\1.php")){ include_once("\\1.php");} ?>', $str);
$str = preg_replace('|{include_htm(.*) (.*)}|isU', '<?php if(is_file("\\2.htm\\1")){ $str = file_get_contents("\\2.htm\\1"); echo $str;} ?>', $str);
$str = preg_replace('|<!--{loop (.*) (.*) (.*)}-->|isU', '<?php if(is_array(\\1)){ foreach(\\1 as \\2 => \\3){ ?>', $str);
$str = preg_replace('|<!--{/loop}-->|isU', '<?php }} ?>', $str);
$str = preg_replace('|{template (.*)}|isU', '<?php include_once(zTemplate("\\1", "'.$templatedir.'", "'.$compliedir.'")); ?>', $str);
@file_put_contents($compliedir.md5($filename).'.php', $str);
}
return $compliedir.md5($filename).'.php';//返回模板结果
}
include_once(zTemplate('template.html','template/', 'templates/'));
?>
用法简单 和dz的用法一样 没有加入语言功能 呵呵 可以自己改 ,没有测bug
<!--{if $val == 's'}-->
{$vals}
<!--{elseif $val == 'ss'}-->
{$vals}
<!--{/if}-->
<br />
{echo "des"}<br />
{$zsh}
<!--{loop $array $key $val}-->
{$val}<br />{$zsh}
<!--{/loop}-->
sed
{template test.htm}
hahah<br />
附件: 您所在的用户组无法下载或查看附件
|  什么才是优秀的程序员呢? |
|