Home  >  Article  >  php教程  >  生成smarttemplate模板处理程序框架

生成smarttemplate模板处理程序框架

WBOY
WBOYOriginal
2016-06-21 09:07:53907browse

程序|模板

/**
* 生成smarttemplate模板处理程序框架
*
* smarttemplate 模板类具有极高的工作效率,代码尺寸很小。
* 他的可扩展模板函数更是有独到之处。
*
* 尽管只有不多的几个方法,使用起来也很简单。
* 但是在多此调整模板文件以后,要使处理程序与模板对应起来可是要花些工夫了。

* 本程序用于分析模板文件、并给出模板处理程序的框架
**/

ob_start();

$Template_name = "include/main.html";
$str = file_get_contents($Template_name);
preg_match_all("/|{\w.*}/U",$str,$regs);

$stack = array();
$vars = array();
$functions = array();
$key = 0; // = chr(ord("i")+count($stack));
$LF = "\r\n";
$outstr = "";
foreach($regs[0] as $v) {
if(preg_match_all("//",$v, $r)) {
array_push($stack,$r[1][0]);
$k = chr(ord("i")+$key++);
$outstr .= str_repeat(chr(9), $key-1)."/* 对数组 \${$r[1][0]} 赋值 */$LF";
$outstr .= str_repeat(chr(9), $key-1)."for(\$$k=0;\$$k }elseif(preg_match_all("//",$v, $r)) {
$v = array_pop($stack);
$outstr .= str_repeat(chr(9), --$key)."}$LF";
if($key) {
$k = chr(ord("i")+$key-1);
$p = $stack[$key-1];
$outstr .= str_repeat(chr(9), $key)."\${$p}[\$$k][$v] = \$$v;$LF";
}else
$outstr .= "\$data[$v] = \$$v;$LF";
}elseif(preg_match_all("/{(\w+)}/",$v, $r)) {
if(count($stack)) {
$v = $stack[$key-1];
$k = chr(ord("i")+$key-1);
$outstr .= str_repeat(chr(9), count($stack))."\${$v}[\$$k][{$r[1][0]}] = '{$r[1][0]}测试';$LF";
$ch = "\$data[".join("][][",$stack)."][{$r[1][0]}]";
if(! in_array($ch, $vars))
$vars[] = $ch;
}else
if(! in_array("\$data[{$r[1][0]}]", $vars)) {
$vars[] = "\$data[{$r[1][0]}]";
$outstr .= "\$data[{$r[1][0]}] = '{$r[1][0]}测试';$LF";
}
}elseif(preg_match_all("/{(\w+):([^}]+)/",$v, $r)) {
$ch = $r[1][0]."({$r[2][0]})";
$define = true;
if(! @include("smarttemplate_extensions/smarttemplate_extension_serverr.php"))
$define = false;
$functions[] = array(name=>$ch, define=>$define);
foreach(split(",",$r[2][0]) as $v)
if($v[0] != '"')
if(! in_array($v, $vars)) {
$vars[] = "\$data[$v]";
$outstr .= "\$data[$v] = '$v测试';$LF";
}
}elseif(preg_match_all("/

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn