function template($file)
{
global $templateDir,$templateTmpDir;
$templateDir ? null : die("not define g_tmplate_dir");
$tplfile = $templateDir.'/'.$file.'.htm';
// print $tplfile;exit(0);
$objfile = $templateTmpDir.'/'.$file.'.tpl.php';
if(!file_exists($tplfile))
{
die("$tplfile template is not exist!");
}
elseif (!file_exists($objfile))
{
parse_template($tplfile,$objfile);
return $objfile;
}
if(@filemtime($tplfile) > @filemtime($objfile))
{
parse_template($tplfile,$objfile);
}
return $objfile;
}
function parse_template($tplfile,$objfile)
{
$view_mod = 0;
$nest = 5;
//print $tplfile;
if(!@$fp = fopen($tplfile, 'r'))
{
exit("Current template file './$tplfile' not found or have no access!");
}
$template = fread($fp, filesize($tplfile));
fclose($fp);
if($view_mod)
{
$template = "<div style='position:relative; width:100px; height:19px; z-index:1;color:white;background:#707888' >$tplfile</div>".$template;
}
$var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\"\'\$\x7f-\xff]+\])*)";
$const_regexp = "([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)";
$template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
$template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
$template = preg_replace("/\{lang\s+(.+?)\}/ies", "languagevar('\\1')", $template);
$template = str_replace("{LF}", "<?=\"\\n\"?>", $template);
$template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/s", "<?=\\1?>", $template);
$template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "addquote('<?=\\1?>')", $template);
$template = "<? if(!defined('IN_SITE')) exit('Access Denied'); ?>\n\n$template";
$template = preg_replace("/\s*\{template\s+(.+?)\}\s*/is", "\n<? include template('\\1'); ?>\n", $template);
$template = preg_replace("/\s*\{d\s+(.+?)\}\s*/is", "<? echo \\1 ?>", $template);
$template = preg_replace("/\s*\{f\s+(.+?)\}\s*/is", "<? \\1 ?>", $template);
$template = preg_replace("/\s*\{eval\s+(.+?)\}\s*/ies", "stripvtags('\n<? \\1 ?>\n','')", $template);
$template = preg_replace("/\s*\{elseif\s+(.+?)\}\s*/ies", "stripvtags('\n<? } elseif(\\1) { ?>\n','')", $template);
$template = preg_replace("/\s*\{else\}\s*/is", "\n<? } else { ?>\n", $template);
for($i = 0; $i < $nest; $i++)
{
$template = preg_replace("/\s*\{loop\s+(\S+)\s+(\S+)\}\s*(.+?)\s*\{\/loop\}\s*/ies", "stripvtags('\n<? if(is_array(\\1)) { foreach(\\1 as \\2) { ?>','\n\\3\n<? } } ?>\n')", $template);
$template = preg_replace("/\s*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}\s*(.+?)\s*\{\/loop\}\s*/ies", "stripvtags('\n<? if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>','\n\\4\n<? } } ?>\n')", $template);
$template = preg_replace("/\s*\{if\s+(.+?)\}\s*(.+?)\s*\{\/if\}\s*/ies", "stripvtags('\n<? if(\\1) { ?>','\n\\2\n<? } ?>\n')", $template);
}
$template = preg_replace("/\{$const_regexp\}/s", "<?=\\1?>", $template);
$template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);
if(!@$fp = fopen($objfile, 'w'))
{
exit("obj template $objfile have no write!");
}
flock($fp, 3);
fwrite($fp, $template);
fclose($fp);
}
function addquote($var) {
return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\]/s", "['\\1']", $var));
}
function stripvtags($expr, $statement) {
$expr = str_replace("\\\"", "\"", preg_replace("/\<\?\=(\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\[\]\"\'\x7f-\xff]*)\?\>/s", "\\1", $expr));
$statement = str_replace("\\\"", "\"", $statement);
return $expr.$statement;
}
?>
评论