<?php
/**
* 生成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("/<!-- begin (\w+) -->/",$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<1;\$k++) \{$lf";
}
elseif(preg_match_all("/<!-- end (\w+) -->/",$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";
}
}
else
if(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";
}
}
else
if(preg_match_all("/<!-- if (\w+)/",$v, $r))
{
if(! in_array("\$data[{$r[1][0]}]", $vars))
{
$vars[] = "\$data[{$r[1][0]}]";
$outstr .= "\$data[{$r[1][0]}] = {$r[1][0]}测试;$lf";
}
}
echo <<< text
<?php
/**
* 模板 $template_name
* 使用了如下变量,请对其赋值
text;
foreach($vars as $v)
echo " * $v$lf";
echo " *$lf";
echo " * 使用了如下模板函数$lf";
$ar = array();
foreach($functions as $k=>$v)
{
echo " * $v[name]$lf";
if(! $v[define]) $ar[] = $k;
}
if(count($ar))
{
$path = "smarttemplate_extensions";
if(! is_dir($path))
{
mkdir($path,0666);
chmod($path,0666);
}
echo " *$lf";
echo " * 其中以下函数未定义,请定义$lf";
foreach($ar as $k)
{
$v = $functions[$k][name];
$name = preg_replace("/(\w+).*/","\\1",$v);
echo " * $v => $path/{$path}_$name.php$lf";
$fp = fopen("$path/{$path}_$name.php","w");
fwrite($fp,"<?php$lf");
fwrite($fp,"function {$path}_$v \{$lf");
fwrite($fp," return 函数$v;$lf");
fwrite($fp,"}$lf?>$lf");
fclose($fp);
}
}
echo <<< text
**/
/** 对模板变量赋值的可能形式 **/
text;
echo $outstr;
echo <<< text
/** 加载并执行模板 **/
require_once "class.smarttemplate.php";
\$page = new smarttemplate("$template_name");
\$page->assign(\$data);
\$page->output();
?>
text;
$code = ob_get_contents();
ob_end_clean();
$fname = basename($template_name);
$fname = substr($fname,0,strrpos($fname,".")+1)."php";
$fp = fopen($fname,"w");
fwrite($fp,$code);
fclose($fp);
include "$fname";
?>
本文作者:未知