tpl_dir=PATH_APP.'template/'; $template_class->cache_dir=PATH_APP.'cache/'; include $template_class->display('header'); '===================================================' */ class Template{ public $tpl_dir;//tpl文件目录 public $cache_dir;//缓存文件目录 //function __construct(){ //} public function display($filename) { $tplfile=$this->tpl_dir."$filename.htm"; $objfile=$this->cache_dir."$filename.php"; if(@filemtime($tplfile) > @filemtime($objfile)) { $this->parse_template($tplfile,$objfile); } return $objfile; } private function parse_template($tplfile,$objfile){ $nest = 5; if(!@$fp = fopen($tplfile, 'r')) { exit("Current template file '$tplfile' not found or have no access!"); } $template = fread($fp, filesize($tplfile)); fclose($fp); $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 = str_replace("{LF}", "", $template); $template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s", "", $template); $template = preg_replace("/$var_regexp/es", "Template::addquote('')", $template); $template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "Template::addquote('')", $template); $template = "\n$template"; $template = preg_replace("/[\n\r\t]*\{eval\s+(.+?)\}[\n\r\t]*/ies", "Template::stripvtags('\n\n','')", $template); $template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "Template::stripvtags('\n\n','')", $template); $template = preg_replace("/[\n\r\t]*\{elseif\s+(.+?)\}[\n\r\t]*/ies", "Template::stripvtags('\n\n','')", $template); $template = preg_replace("/[\n\r\t]*\{else\}[\n\r\t]*/is", "\n\n", $template); for($i = 0; $i < $nest; $i++) { $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r]*(.+?)[\n\r]*\{\/loop\}[\n\r\t]*/ies", "Template::stripvtags('\n','\n\\3\n\n')", $template); $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/ies", "Template::stripvtags('\n \\3) { ?>','\n\\4\n\n')", $template); $template = preg_replace("/[\n\r\t]*\{if\s+(.+?)\}[\n\r]*(.+?)[\n\r]*\{\/if\}[\n\r\t]*/ies", "Template::stripvtags('\n','\n\\2\n\n')", $template); } $template = preg_replace("/\{$const_regexp\}/s", "", $template); $template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template); if(!@$fp = fopen($objfile, 'w')) { exit("Directory '$this->cache_dir' not found or have no access!"); } $template = preg_replace("/\"(http)?[\w\.\/:]+\?[^\"]+?&[^\"]+?\"/e", "Template::transamp('\\0')", $template); $template = preg_replace("/\]*?src=\"(.+?)\".*?\>\s*\<\/script\>/ise", "Template::stripscriptamp('\\1')", $template); flock($fp, 2); fwrite($fp, $template); fclose($fp); } private function transamp($str) { $str = str_replace('&', '&', $str); $str = str_replace('&amp;', '&', $str); $str = str_replace('\"', '"', $str); return $str; } private function addquote($var) { return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var)); } private function stripvtags($expr, $statement) { $expr = str_replace("\\\"", "\"", preg_replace("/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr)); $statement = str_replace("\\\"", "\"", $statement); return $expr.$statement; } private function stripscriptamp($s) { $s = str_replace('&', '&', $s); return ""; } } ?>