当前位置:编程问答 > php >

生成smarttemplate模板处理程序框架

答案:<?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";
}
}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("/<!-- 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";

?>

上一个:A HOWTO on Optimizing PHP
下一个:php+mysql分页处理的探讨

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,