简单的模板函数
define('DEFAULT_TEMPLATE', 'templates');
define('DEFAULT_TEMPLATE_C', 'templates_c');
define('TEMPLATE_DEPLOY', true);
function template($FileName, $TemplateDir='', $ComplieDir='', $op = ''){
$str = '';
$TemplateDir = empty($TemplateDir) ? DEFAULT_TEMPLATE : $TemplateDir;
$ComplieDir = empty($ComplieDir) ? DEFAULT_TEMPLATE_C : $ComplieDir;
if(TEMPLATE_DEPLOY === false) {
$comtime = @filemtime($ComplieDir . "/" . md5($FileName).".php");
$temtime = @filemtime($TemplateDir . "/" . $FileName);
$str = file_get_contents($TemplateDir . "/" . $FileName);
$checktemplate = preg_match('|{template (.*)}|isU', $str);
}
if(($temtime > $comtime && TEMPLATE_DEPLOY === false) || $checktemplate){
if(empty($str)) {
$str = file_get_contents($TemplateDir . "/" . $FileName);
}
$str = preg_replace('||isU', '\1){ ?>', $str);
$str = preg_replace('||isU', '', $str);
$str = preg_replace('|{_(.*)}|isU', '\1; ?>', $str);
$str = preg_replace('|{$lang_(.*)}|isU', '\1"]; ?>', $str);
$str = preg_replace('|{$(.*)}|isU', '', $str);
$str = preg_replace('||isU', '', $str);
$str = preg_replace('||isU', '\1){ ?>', $str);
$str = preg_replace('|{eval (.*)}|isU', '\1; ?>', $str);
$str = preg_replace('|{echo (.*)}|isU', '\1; ?>', $str);
if(preg_match_all('|{include_php (.*)}|isU', $str, $phparray)) {
$phpfilename = $phparray[1][0] . ".php";
if(is_file($phpfilename)) {
$phpcontent = str_replace("
$phpcontent = str_replace("?>", " ?>", $phpcontent);
$phpcontent = str_replace("", "
$str = $str . $phpcontent;
}
$str = preg_replace('|{include_php (.*)}|isU', '', $str);
}
$str = preg_replace('|{include_htm(.*) (.*)}|isU', '', $str);
$str = preg_replace('||isU', '\1)){ foreach(\1 as \2 => \3){ ?>', $str);
$str = preg_replace('||isU', '\1) { ?>', $str);
$str = preg_replace('||isU', '', $str);
$str = preg_replace('||isU', '', $str);
if(preg_match_all('|{template (.*)}|isU', $str, $templatearray)) {
$str = preg_replace('|{template (.*)}|isU', '', $str);
$templatefilename = $templatearray[1][0];
if(is_file($TemplateDir . '/' .$templatefilename)) {
$templatecontent = template($templatefilename, $TemplateDir, $ComplieDir, 1);
$str = $str . $templatecontent;
}
}
$str = preg_replace('|t|isU', '', $str);
$str = preg_replace('|rn|isU', '', $str);
$str = preg_replace('|n|isU', '', $str);
$str = preg_replace('|(?>
if(!$op) {
@file_put_contents($ComplieDir . "/" . md5($FileName) . '.php', $str);
}
}
if($op) {
return $str;
}
return $ComplieDir . "/" . md5($FileName) . '.php';
}