上节介绍过,v1.2.0使用的是正则表达式的方式来解析模板里的标签的,熟悉DT系统的人应该比较了解这种模板引擎。和dedecms的模板引擎比较,这种方式解析速度更快些,dedecms采用的是一种类似编译器...
function setVar($tagname,$tagvar,$boolphp = false) { if($boolphp == true) $tagvar = '<?php '.$tagvar.' ?>'; $this->outstring = str_replace("{zengl ". $tagname ."}", $tagvar, $this->outstring); //通过str_replace函数将模板中的{zengl .....}标签替换为$tagvar参数对应的PHP代码 } |
function template_parse() { $str = $this->outstring; $str = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $str); //将<!--{....}-->替换为{....} $str = preg_replace("/\{template\s+([^\}]+)\}/", "<?php include template(\\1);?>", $str); //将{template(....)}替换为<?php include template(....);?> $str = preg_replace("/\{php\s+(.+)\}/", "<?php \\1?>", $str); //将{php .....} 替换为 <?php ...... ?> $str = preg_replace("/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $str); //将{if ......} 替换为 <?php if(......){ ?> $str = preg_replace("/\{else\}/", "<?php } else { ?>", $str); //将{else}标签替换为 <?php } else { ?> $str = preg_replace("/\{elseif\s+(.+?)\}/", "<?php } else if(\\1) { ?>", $str); //将{elseif .....}替换为<?php } else if(.....) ?> $str = preg_replace("/\{\/if\}/", "<?php } ?>", $str); //将{/if}替换为<?php } ?> $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/", "<?php if(is_array(\\1)) { foreach(\\1 as \\2) { ?>", $str); //将{loop .... ....}替换为 <?php if(is_array(....)) { foreach(..... as .....) { ?> $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<?php if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>", $str); //将{loop .... .... ....}替换为<?php if(is_array(....)) { foreach(..... as ..... => ....) { ?> $str = preg_replace("/\{\/loop\}/", "<?php } } ?>", $str); //将{/loop} 替换为 <?php } } ?> $str = preg_replace("/\{while\s+(.+?)\}/", "<?php while(\\1) { ?>", $str); //将{while ....}替换为<?php while(....) { ?> $str = preg_replace("/\{\/while\}/", "<?php } ?>", $str); //将{/while}替换为 <?php } ?> $str = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str); //将类似{test(a,b)}这样的函数标签替换为<?php echo test(a,b);?>之类的 $str = preg_replace("/<\?php([^\?]+)\?>/es", "template_addquote('<?php\\1?>')", $str); //对PHP里的数组元素添加必要的引号 $str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "<?php echo \\1;?>", $str); //将类似{$test}这样的变量标签替换为<?php echo $test;?> $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\->\$\x7f-\xff]+)\}/es", "template_addquote('<?php echo \\1;?>')", $str); //为类成员添加必要的引号 $str = preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>", $str); //将类似{MACRO}这样的常量标签替换为<?php echo MACRO;?> $str = preg_replace("/\'([A-Za-z]+)\[\'([A-Za-z\.]+)\'\](.?)\'/s", "'\\1[\\2]\\3'", $str); //将类似'abc['abc']'这样的标签替换为'abc[abc]' $str = preg_replace("/(\r?\n)\\1+/", "\\1", $str); //将多个连续的\r\n或多个连续的\n进行合并 $str = str_replace("\t", '', $str); //将tab制表符替换为空字符串 $this->outstring = $str; return $str; } |