uchome分离出来的模板引擎及测试例子
MVC(Model View Controller)模型-视图-控制器,作为软件开发的一个重要理念,在php的各框架中都有体现,而模板引擎就是MVC模式建立过程的重要方法,smarty就是最出名的一个模板引擎。这里介绍一个更轻量的模板引擎,从uchome中分离出来的。
目录如下,cache下存放缓存数据(默认空),template存放模板文件,function_template.php为模板引擎解析类,运行test.php即可。
程序首先查询缓存文件是否存在,存在则读取缓存,否则读取模板文件,解析模板文件并生成缓存。
template下存放三个文件,分别为header.htm、footer.htm、test.htm。
代码如下:
header.htm:
<b>this is header</b>
footer.htm:
<b>this is footer<b>
test.htm:
1.包含头页面:<br />
<!–{template header}–><br />
<!–{eval echo “<hr>”}–>
2.时间date函数<br />
<!–{date(‘Y-m-d’,1212121111,1)}–><br /><br />
3.模板执行php代码测试:<br />
<!–{eval echo date(‘Y-m-d’,time())}–><br /><br />
4.变量:<br />
<!–{$var}–><br /><br />
5.常量:<br />
<!–{__FILE__}–><br /><br />
5.if else 判断<br />
<div id=”content”>
<font color=’red’>执行if测试:</font></font><br />
<!–{if $t>5}–>
t>5
<!–{/if}–>
</br>
<font color=’red’>执行if else 测试:</font><br />
<!–{if $t>5}–>
t大于5
<!–{else}–>
t小于5
<!–{/if}–>
</div>
<br />
6.循环测试:<br />
<!–{loop $testarray $k $v}–>
{$k}:{$v} <br />
<!–{/loop}–>
<br />
7.包含footer页面:<br />
<!–{template footer}–>
test.php:
<?php
define(‘S_ROOT’,dirname(__FILE__)); //根路径
define(‘TEMPLATE_PATH’,'template/’); //模板目录
//测试变量
$var=”var–LIYINGBOfffffddddddddddd”;
//测试判断
$t = 14;
//测试循环
$testarray = array(1,2,3,4,5);
include(‘function_template.php’);
include_once template(“test”);
?>
function_template.php
<?php
/*
[UCenter Home] (C) 2007-2008 Comsenz Inc.
拆分出来的uchome模板框架 -MVC modify by liyingbo 2009-6-28
*/
function template($name) {
$tpl = TEMPLATE_PATH.$name;
$objfile = S_ROOT.’/cache/’.str_replace(‘/’,'_’,$tpl).’.php’;
//不存在缓存文件则解析模板
if(!file_exists($objfile)) {
parse_template($tpl);
}
return $objfile;
}
//包含解析,通过preg_replace替代
function parse_template($tpl) {
$tplfile = S_ROOT.’/’.$tpl.’.htm’;
$objfile = S_ROOT.’/cache/’.str_replace(‘/’,'_’,$tpl).’.php’;
//read模板
$template = sreadfile($tplfile);
if(empty($template)) {
exit(“Template file : $tplfile Not found or have no access!”);
}
//开始解析……
//模板
$template = preg_replace(“/\<\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\>/ie”, “readtemplate(‘\\1′)”, $template);
//时间处理
$template = preg_replace(“/\<\!\-\-\{date\((.+?)\)\}\-\-\>/ie”, “datetags(‘\\1′)”, $template);
//PHP代码
$template = preg_replace(“/\<\!\-\-\{eval\s+(.+?)\s*\}\-\-\>/ies”, “evaltags(‘\\1′)”, $template);
//变量
$var_regexp = “((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)”;
$template = preg_replace(“/\<\!\-\-\{(.+?)\}\-\-\>/s”, “{\\1}”, $template);
$template = preg_replace(“/([\n\r]+)\t+/s”, “\\1“, $template);
$template = preg_replace(“/(\\\$[a-zA-Z0-9_\[\]\’\”\$\x7f-\xff]+)\.([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/s“, “\\1['\\2']“, $template);
$template = preg_replace(“/\{(\\\$[a-zA-Z0-9_\[\]\’\”\$\.\x7f-\xff]+)\}/s“, “<?=\\1?>”, $template);
$template = preg_replace(“/$var_regexp/es”, “addquote(‘<?=\\1?>’)”, $template);
$template = preg_replace(“/\<\?\=\<\?\=$var_regexp\?\>\?\>/es”, “addquote(‘<?=\\1?>’)”, $template);
//逻辑
$template = preg_replace(“/\{elseif\s+(.+?)\}/ies”, “stripvtags(‘<?php } elseif(\\1) { ?>’,”)”, $template);
$template = preg_replace(“/\{else\}/is”, “<?php } else { ?>”, $template);
//循环
for($i = 0; $i < 5; $i++) {
$template = preg_replace(“/\{loop\s+(\S+)\s+(\S+)\}(.+?)\{\/loop\}/ies”, “stripvtags(‘<?php if(is_array(\\1)) { foreach(\\1 as \\2) { ?>’,'\\3<?php } } ?>’)”, $template);
$template = preg_replace(“/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}(.+?)\{\/loop\}/ies”, “stripvtags(‘<?php if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>’,'\\4<?php } } ?>’)”, $template);
$template = preg_replace(“/\{if\s+(.+?)\}(.+?)\{\/if\}/ies”, “stripvtags(‘<?php if(\\1) { ?>’,'\\2<?php } ?>’)”, $template);
}
//常量
$template = preg_replace(“/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s”, “<?=\\1?>”, $template);
//write模板
if(!swritefile($objfile, $template)) {
exit(“File: $objfile can not be write!”);
}
}
function addquote($var) {
return str_replace(“\\\“”, “\”", preg_replace(“/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s”, “['\\1']“, $var));
}
function evaltags($php) {
return “<?php “.stripvtags($php).” ?>”;
}
function datetags($parameter) {
return “<?php echo sgmdate($parameter); ?>”;
}
function stripvtags($expr, $statement=”) {
$expr = str_replace(“\\\“”, “\”", preg_replace(“/\<\?\=(\\\$.+?)\?\>/s“, “\\1“, $expr));
$statement = str_replace(“\\\“”, “\”", $statement);
return $expr.$statement;
}
//模板中套模板文件
function readtemplate($name) {
$tpl = strpos($name,’/')?$name:TEMPLATE_PATH.”$name”;
$file = S_ROOT.’/’.$tpl.’.htm’;
$content = sreadfile($file);
return $content;
}
//获取文件内容
function sreadfile($filename) {
$content = ”;
if(function_exists(‘file_get_contents’)) {
@$content = file_get_contents($filename);
} else {
if(@$fp = fopen($filename, ‘r’)) {
@$content = fread($fp, filesize($filename));
@fclose($fp);
}
}
return $content;
}
//写入文件
function swritefile($filename, $writetext, $openmod=’w') {
if(@$fp = fopen($filename, $openmod)) {
flock($fp, 2);
fwrite($fp, $writetext);
fclose($fp);
return true;
} else {
return false;
}
}
//时间格式化
function sgmdate($dateformat, $timestamp=”, $format=0) {
if(empty($timestamp)) {
$timestamp = time();
}
$result = ”;
if($format) {
$time = time() – $timestamp;
if($time > 24*3600) {
$result = gmdate($dateformat, $timestamp);
} elseif ($time > 3600) {
$result = intval($time/3600).lang(‘hour’).lang(‘before’);
} elseif ($time > 60) {
$result = intval($time/60).lang(‘minute’).lang(‘before’);
} elseif ($time > 0) {
$result = $time.lang(‘second’).lang(‘before’);
} else {
$result = lang(‘now’);
}
} else {
$result = gmdate($dateformat, $timestamp);
}
return $result;
}
?>

