]*)(\[\'[a-zA-Z0-9_\.\"\'\[\]\$]+?\'\]))}#', '\1', $s);
if (!$u) $s = preg_replace('#echo "(.+?)";#e', "'echo \"'.\$this->b('\\1').'\";'", $s);
return "]];\n".$s."\necho [[";
}
function b($s) {
$s = str_replace('\\\"', '"', $this->c($s));
$s = preg_replace('#(\$[a-zA-Z_][a-zA-Z0-9_\->]*\[\'[a-zA-Z0-9_\.\"\'\[\]\$]+?\'\])#', '{\1}', $s);
return $s;
}
function c($v) {
return preg_replace("/\[([a-zA-Z0-9_\.]+)\]/s", "['\\1']", $v);
}
function d($s) {
$s = str_replace('\\\"', '"', $s);
$s = str_replace('\'', '\\\'', $s);
$s = str_replace('\\\\\'', '\\\\\\\'', $s);
$s = preg_replace('#(\$[a-zA-Z_][a-zA-Z0-9_\->]*(\[[a-zA-Z0-9_\.\[\]\$]+?\])*)#', '\'.\1.\'', $s);
$s = str_replace(']].\'', '].\']', $s);
$s = str_replace('.\'\'.', '.', $s);
return '\''.$s.'\';';
}
function e() {
if (!file_exists($this->userpack)) $this->error(2);
include $this->userpack;
if (is_array($data)) {
$this->template = preg_replace('/{\#(.+?)\#}/e', "\$data['\\1'];", $this->template);
}
}
//模板的编译
function compile() {
$this->template = preg_replace('#\t*<(!--)?require:(\S+?)-->#ie', 'file_get_contents($this->tplpath."\2")', $this->template);
$var = '(\$[a-zA-Z_][a-zA-Z0-9_\->\.\[\]\$]*)';
$this->template = str_replace('"', '\"', $this->template);
if ($this->userpack != '') $this->e();
//模板语法
$find[] = '#<\?.*?\?>#is';
$replace[] = '';
$find[] = '##s';
$replace[] = '';
$find[] = '#{\[(\S+?)\]}#';
$replace[] = '$_v_[\1]';
$find[] = '#\t*<(!--)?if:(.+?)-->#ie';
$replace[] = '$this->a(\'if (\2) {\')';
$find[] = '#\t*<(!--)?else-->#ie';
$replace[] = '$this->a(\'} else {\')';
$find[] = '#\t*<(!--)?elseif:(.+?)-->#ie';
$replace[] = '$this->a(\'} elseif (\\2) {\')';
$find[] = '#\t*<(!--)?switch:(.+?)-->#ie';
$replace[] = '$this->a(\'switch (\2) {default:\')';
$find[] = '#\t*<(!--)?case:(.+?)-->#ie';
$replace[] = '$this->a(\'break;case "\2":\')';
$find[] = '#\t*<(!--)?for:(\S+?)\s+(\S+?)-->#ie';
$replace[] = '$this->a(\'if(is_array(\2)) foreach (\2 as \3) {\')';
$find[] = '#\t*<(!--)?for:(\S+?)\s+(\S+?)\s+(\S+?)-->#ie';
$replace[] = '$this->a(\'if(is_array(\2)) foreach (\2 as \3 => \4) {\')';
$find[] = '#\t*<(!--)?for_option:(\S+?)\s+(\S+?)\s+(\S+?)\s+(\S+?)-->#ie';
$replace[] = '$this->a(\'if(is_array(\2)) foreach (\2 as $_k_ => \4) {echo \\\'\\\';\')';
$find[] = '#\t*<(!--)?checkbox\s+(\S+?):(\S+?)\s+(\S+?)-->#ie';
$replace[] = '$this->a(\'if(is_array(\3)) foreach (\3 as $_k_ => $_v_) echo \\\'\\\'.$_v_;\')';
$find[] = '#\t*<(!--)?radio\s+(\S+?):(\S+?)\s+(\S+?)-->#ie';
$replace[] = '$this->a(\'if(is_array(\3)) foreach (\3 as $_k_ => $_v_) echo \\\'\\\'.$_v_;\')';
$obs_count = 0;
$find[] = '#\t*<(!--)?htmlcache-->#ie';
$replace[] = '$this->a(\'/*templates_OB_Start_\'.(++$obs_count).\'*/ob_start();\')';
$obe_count = 0;
$find[] = '#\t*<(!--)?/htmlcache-->#ie';
$replace[] = '$this->a(\'\$ob_\'.(++$obe_count).\'=ob_get_contents();ob_end_clean();echo \$ob_\'.$obe_count.\';/*templates_OB_End*/\')';
//更多语法请您自由添加 ^_^
//编译文件结构调整
$this->template = preg_replace($find, $replace, $this->template);
$find = array('#<(!--)?/if-->#ie', '#<(!--)?/switch-->#ie', '#<(!--)?/for-->#ie', '#<(!--)?/for_option-->#ie', '#<(!--)?/for_checkbox-->#ie', '#<(!--)?/for_radio-->#ie', '#<(!--)?/loop-->#ie', '#<(!--)?/func-->#ie');
$this->template = preg_replace($find, '$this->a(\'}\')', $this->template);
$this->template = preg_replace('#echo "\s*";#is', '', "echo [[".$this->template."]];");
$this->template = preg_replace('#\[\[(.*?)\]\];#es', '$this->d(\'\1\')', $this->template);
$this->template = "template."\n?>";
$this->template = str_replace('echo \'\'.', 'echo ', $this->template);
$find = array('#((\$[a-zA-Z_][a-zA-Z0-9_\->]*)(\[[a-zA-Z0-9_\.\[\]\$]+\])+)#e', '#echo \'\s+\';#', '#(\r|\n)+#', '#{(\'\.\$[a-zA-Z_][a-zA-Z0-9_\->]*(\[[a-zA-Z0-9_\.\[\]\$]+?\])*\.\')}#');
$replace = array('$this->c(\'\1\')', '', '\1', '\1');
$this->template = preg_replace($find, $replace, $this->template);
if ($obs_count) {
$this->template .= '/*templates_OB_Script*/$fp=fopen(__FILE__, \'rb\');$selfcontent=fread($fp,filesize(__FILE__));fclose($fp);$selfcontent=preg_replace("/\/\*templates_OB_Start_(\d+)\*\/.+?\/\*templates_OB_End\*\//ies","\'?>\'.\$ob_\\\\1.\'\'",$selfcontent);$selfcontent=preg_replace("/<\?\/\*templates_OB_Script\*\/.+?\/\*templates_OB_Script\*\/\?>/is",\'\',$selfcontent);$fp=fopen(__FILE__,\'wb\');@flock($fp,LOCK_EX);fwrite($fp,$selfcontent);@flock($fp,LOCK_UN);fclose($fp);/*templates_OB_Script*/?>';
}
//写入编译文件
if (!$fp = @fopen($this->cachefile, 'wb')) $this->error(1);
@flock($fp, LOCK_EX);
fwrite($fp, $this->template);
@flock($fp, LOCK_UN);
fclose($fp);
return $this->cachefile;
}
}
?>