*作者:旭日EMAIL:54ano@163.comhttp:www.wrcms.com使用方法:解析的方法来自DISCUZ的template.class.phpdefine
/*
作者:旭日
EMAIL:54ano@163.com
http://www.wrcms.com
使用方法:解析的方法来自DISCUZ的template.class.php
define(T_ROOT, dirname(__FILE__));定义根目录
$tpl = new Template(T_ROOT, T_ROOT.'/default', 'index', 'index');声明类
$tpl->getCacheDir(T_ROOT.'./Cache');声明缓存目录
$tpl->setVar('sitename', $sitename);声明变量
$tpl->Display();显示
$tpl->Save();保存成目标文件
*/
class Template {
var $pClassName = 'PTS Template';//类名称
var $pClassVersion = '';//版本
var $pTplDir = '';//模板目录
var $pTplFileExt = '.htm';//模板文件扩展名
var $pTplFileName = '';//模板文件名
var $pObjFileExt = '.html';//目标文件扩展名
var $pObjDir = '';//目标目录
var $pObjFileName = '';//目标文件名
var $pCacheDir = './Cache';//缓存目录
var $pVarval = array();
var $pContent = '';
function Template($ObjDir, $TplDir, $ObjFileName, $TplFileName, $ObjFileExt='.html', $TplFileExt='.htm') {
$this->getObjDir($ObjDir);
$this->getTplDir($TplDir);
$this->getObjFileName($ObjFileName);
$this->getTplFileName($TplFileName);
$this->getObjFileExt($ObjFileExt);
$this->getTplFileExt($TplFileExt);
}
function getTplDir($value) {
$this->pTplDir = $value;
}
function getTplFileName($value) {
$this->pTplFileName = $value;
}
function getTplFileExt($value) {
$this->pTplFileExt = $value;
}
function getObjFileName($value) {
$this->pObjFileName = $value;
}
function getObjDir($value) {
$this->pObjDir = $value;
}
function getObjFileExt($value) {
$this->pObjFileExt = $value;
}
function getCacheDir($value) {
$this->pCacheDir = $value;
}
function setVar($varname, $value = '') {
$this->pVarval[$varname] = $value;
}
function Display() {
extract($this->pVarval);
$this->parseToCache($this->pTplFileName);
include $this->pCacheDir.'/'.$this->pTplFileName.'.tpl.php';
}
function Save() {
ob_start();
$this->Display();
$content = ob_get_contents();
ob_end_clean();
$p = fopen($this->pObjDir.'/'.$this->pObjFileName.$this->pObjFileExt, 'w');
fwrite($p, $content);
fclose($p);
}
function parseToCache($TplFileName) {
$p = fopen($this->pTplDir.'/'.$TplFileName.$this->pTplFileExt, 'r');
$this->pContent = fread($p, filesize($this->pTplDir.'/'.$TplFileName.$this->pTplFileExt));
fclose($p);
$this->Parse();
$p = fopen($this->pCacheDir.'/'.$TplFileName.'.tpl.php', 'w');
fwrite($p, $this->pContent);
fclose($p);
}
function Parse() {
extract($this->pVarval);
$nest = 5;
$this->pContent = preg_replace("/([\n\r]+)\t+/s", "\\1", $this->pContent);
$this->pContent = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $this->pContent);
$this->pContent = str_replace("{LF}", "=\"\\n\"?>", $this->pContent);
$this->pContent = preg_replace("/\{((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)\}/is", "\n=\\1?>\n", $this->pContent);
$this->pContent = preg_replace("/[\n\r\t]*\{include\s+([a-z0-9_]+)\}[\n\r\t]*/ies", "Template::parseInclude('\\1')", $this->pContent);
$this->pContent = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/is", "\n echo \\1; ?>\n", $this->pContent);
$this->pContent = preg_replace("/[\n\r\t]*\{elseif\s+(.+?)\}[\n\r\t]*/is", "\n } elseif(\\1) { ?>\n", $this->pContent);
$this->pContent = preg_replace("/[\n\r\t]*\{else\}[\n\r\t]*/is", "\n } else { ?>\n", $this->pContent);
for($i = 0; $i < $nest; $i++) {
$this->pContent = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r]*(.+?)[\n\r]*\{\/loop\}[\n\r\t]*/is", "\n if(is_array(\\1)) { foreach(\\1 as \\2) { ?>\n\\3\n } } ?>\n", $this->pContent);
$this->pContent = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/is", "\n if(is_array(\\1)) {\n foreach(\\1 as \\2 => \\3) { ?>\n\\4\n } } ?>\n", $this->pContent);
$this->pContent = preg_replace("/[\n\r\t]*\{if\s+(.+?)\}[\n\r]*(.+?)[\n\r]*\{\/if\}[\n\r\t]*/is", "\n if(\\1) { ?>\n\\2\n } ?>\n", $this->pContent);
}
$this->pContent = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s", "=\\1?>", $this->pContent);
}
function parseInclude($value) {
echo 'aa';
$this->parseToCache($value);
return '';
}
}
3 个解决方案
没什么意思,有能力就弄一个像织梦CMS里的模块解析引擎那么强大的东西