从康盛产品(discuz)提取出来的模板类


Posted in PHP onJune 28, 2011
<?php 
/*template.class.php 
@康盛微博 模板提取类 觉得这个模板好用 花些时间独立出来。 by 雷日锦 
@看了一下ctt 这个模板 跟 phpcms的模板类似 难道?? ^_^ 嘿嘿!!! 
@ 微博 http://weibo.com/lrjxgl 
@ 好东西大家共享 磕磕绊绊的提取出来 有问题请提出来 
@ 模板文件默认为 .htm 
$tpl = new template('skin',"default"); 
$tpl->objdir='tpp'; 
$tpl->rewrite=true;//开启rewrite 需要服务器支持 
$tpl->rewrite_rule=array(array("/index\.php/"),array("index.html")); //rewrite规则 
$tpl->assign("indexurl","index.php"); 
$tpl->assign("str","我是字符串啦啦啦"); 
$tpl->assign("ec","我是被echo出来的"); 
$tpl->assign("subhtml","{subtpl ttt}这是用来引入一个模板文件的,这个就是引入ttt.htm"); 
$tpl->assign("a",array('dasdasd'.'bbbbbbb','cccccccccccccc')); 
$tpl->assign("i",1); 
$tpl->display("index"); 
*/ 
if(!defined("CHARSET")) define("CHARSET","gb2312");//字符编码 
if(!defined("DIR_TPL")) define("DIR_TPL","tpl");//默认模板目录 
if(!defined("DIR_DATA")) define("DIR_DATA","data");//默认数据目录 
if(!defined("DEBUG")) define("DEBUG",0);//默认运行模式 
class template { 
//note var 
public $rewrite=false;//是否开启 伪静态 rewrite 
public $rewrite_rule=array(); //设置伪静态规则 
public $defaulttpldir;//默认的模板 
public $tpldir;//模板目录 
public $objdir;//编译缓存目录 
public $tplfile;//模板文件 
public $objfile;//编译文件 
public $tplid=1;//模板编号 
public $currdir='default';//当前风格目录 
public $vars=array();//note 变量表 
public $removeblanks=false;//移除空格 
public $stdout='display';//输出类型 
function __construct($tplid, $currdir) { 
$this->template($tplid, $currdir); 
} 
function template($tplid, $currdir) { 
ob_start(); 
if(file_exists(DIR_TPL.'/'.$currdir)) { 
$this->currdir = $currdir; 
$this->tplid = $tplid; 
} else { 
$this->currdir = 'default'; 
$this->tplid = 1; 
} 
$this->defaulttpldir = DIR_TPL.'/default'; 
$this->tpldir = DIR_TPL.'/'.$this->currdir; 
$this->objdir = DIR_DATA.'/cache/tpl'; 
if(version_compare(PHP_VERSION, '5') == -1) { 
register_shutdown_function(array(&$this, '__destruct')); 
} 
} 
//note publlic 
function assign($k, $v) { 
$this->vars[$k] = $v; 
} 
//note publlic 
function display($file) { 
extract($this->vars, EXTR_SKIP); 
include $this->getObj($file); 
} 
function getObj($file, $tpldir = '') { 
$subdir = ($pos = strpos($file, '/')) === false ? '' : substr($file, 0, $pos); 
$file = $subdir ? substr($file, $pos + 1) : $file; 
$this->tplfile = ($tpldir ? $tpldir : $this->tpldir).'/'.($subdir ? $subdir.'/' : '').$file.'.htm'; 
$this->objfile = $this->objdir.'/'.($tpldir ? '' : $this->tplid.'_').($subdir ? $subdir.'_' : '').$file.'.php'; 
//note 默认目录 
if(@filemtime($this->tplfile) === FALSE) { 
$this->tplfile = $this->defaulttpldir.'/'.($subdir ? $subdir.'/' : '').$file.'.htm'; 
} 
//note 判断是否比较过期 
if(!file_exists($this->objfile) || DEBUG && @filemtime($this->objfile) < filemtime($this->tplfile)) { 
$this->compile(); 
} 
return $this->objfile; 
} 
function getTpl($file) { 
$subdir = ($pos = strpos($file, '/')) === false ? '' : substr($file, 0, $pos); 
$file = $subdir ? substr($file, $pos + 1) : $file; 
$tplfile = $this->tpldir.'/'.($subdir ? $subdir.'/' : '').$file.'.htm'; 
if(@filemtime($tplfile) === FALSE) { 
$tplfile = $this->defaulttpldir.'/'.($subdir ? $subdir.'/' : '').$file.'.htm'; 
} 
return $tplfile; 
} 
function compile() { 
$var_regexp = "\@?\\\$[a-zA-Z_]\w*(?:\[[\w\.\"\'\[\]\$]+\])*"; 
$vtag_regexp = "\<\?=(\@?\\\$[a-zA-Z_]\w*(?:\[[\w\.\"\'\[\]\$]+\])*)\?\>"; 
$const_regexp = "\{([\w]+)\}"; 
$template = file_get_contents($this->tplfile); 
for($i = 1; $i <= 3; $i++) { 
if(strpos($template, '{subtpl') !== FALSE) { 
if(DEBUG == 2) { 
$template = str_replace('{subtpl ', '{tpl ', $template); 
} else { 
$template = preg_replace("/[\n\r\t]*\{subtpl\s+([a-z0-9_:\/]+)\}[\n\r\t]*/ies", "file_get_contents(\$this->getTpl('\\1'))", $template); 
} 
} 
} 
$remove = array( 
'/(^|\r|\n)\/\*.+?(\r|\n)\*\/(\r|\n)/is', 
'/\/\/note.+?(\r|\n)/i', 
'/\/\/debug.+?(\r|\n)/i', 
'/(^|\r|\n)(\s|\t)+/', 
'/(\r|\n)/', 
); 
$this->removeblanks && $template = preg_replace($remove, '', $template); 
$template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template); 
$template = preg_replace("/\{($var_regexp)\}/", "<?=\\1?>", $template); 
$template = preg_replace("/\{($const_regexp)\}/", "<?=\\1?>", $template); 
$template = preg_replace("/(?<!\<\?\=|\\\\)$var_regexp/", "<?=\\0?>", $template); 
$template = preg_replace("/\<\?=(\@?\\\$[a-zA-Z_]\w*)((\[[\\$\[\]\w]+\])+)\?\>/ies", "\$this->arrayindex('\\1', '\\2')", $template); 
$template = preg_replace("/\{\{eval (.*?)\}\}/ies", "\$this->stripvtag('<? \\1?>')", $template); 
$template = preg_replace("/\{eval (.*?)\}/ies", "\$this->stripvtag('<? \\1?>')", $template); 
$template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "\$this->stripvtag('<? echo \\1; ?>','')", $template); 
$template = preg_replace("/\{for (.*?)\}/ies", "\$this->stripvtag('<? for(\\1) {?>')", $template); 
$template = preg_replace("/\{elseif\s+(.+?)\}/ies", "\$this->stripvtag('<? } elseif(\\1) { ?>')", $template); 
for($i=0; $i<2; $i++) { 
$template = preg_replace("/\{loop\s+$vtag_regexp\s+$vtag_regexp\s+$vtag_regexp\}(.+?)\{\/loop\}/ies", "\$this->loopsection('\\1', '\\2', '\\3', '\\4')", $template); 
$template = preg_replace("/\{loop\s+$vtag_regexp\s+$vtag_regexp\}(.+?)\{\/loop\}/ies", "\$this->loopsection('\\1', '', '\\2', '\\3')", $template); 
} 
$template = preg_replace("/\{if\s+(.+?)\}/ies", "\$this->stripvtag('<? if(\\1) { ?>')", $template); 
$template = preg_replace("/\{tpl\s+(\w+?)\}/is", "<? include \$this->getObj(\"\\1\");?>", $template); 
$template = preg_replace("/\{tpl\s+(.+?)\}/ise", "\$this->stripvtag('<? include \$this->getObj(\"\\1\"); ?>')", $template); 
$template = preg_replace("/\{tmptpl\s+(\w+?)\}/is", "<? include \$this->getObj(\"\\1\", \$this->objdir);?>", $template); 
$template = preg_replace("/\{tmptpl\s+(.+?)\}/ise", "\$this->stripvtag('<? include \$this->getObj(\"\\1\", \$this->objdir); ?>')", $template); 
$template = preg_replace("/\{else\}/is", "<? } else { ?>", $template); 
$template = preg_replace("/\{\/if\}/is", "<? } ?>", $template); 
$template = preg_replace("/\{\/for\}/is", "<? } ?>", $template); 
$template = preg_replace("/$const_regexp/", "<?=\\1?>", $template);//note {else} 也符合常量格式,此处要注意先后顺序 
$template = preg_replace("/(\\\$[a-zA-Z_]\w+\[)([a-zA-Z_]\w+)\]/i", "\\1'\\2']", $template); 
$fp = fopen($this->objfile, 'w'); 
fwrite($fp, $template); 
fclose($fp); 
} 
function arrayindex($name, $items) { 
$items = preg_replace("/\[([a-zA-Z_]\w*)\]/is", "['\\1']", $items); 
return "<?=$name$items?>"; 
} 
function stripvtag($s) { 
$vtag_regexp = "\<\?=(\@?\\\$[a-zA-Z_]\w*(?:\[[\w\.\"\'\[\]\$]+\])*)\?\>"; 
return preg_replace("/$vtag_regexp/is", "\\1", str_replace("\\\"", '"', $s)); 
} 
function loopsection($arr, $k, $v, $statement) { 
$arr = $this->stripvtag($arr); 
$k = $this->stripvtag($k); 
$v = $this->stripvtag($v); 
$statement = str_replace("\\\"", '"', $statement); 
return $k ? "<? foreach((array)$arr as $k => $v) {?>$statement<?}?>" : "<? foreach((array)$arr as $v) {?>$statement<? } ?>"; 
} 
function __destruct() { 
$content = ob_get_contents(); 
//处理 rewrite 
if($this->rewrite) { 
$arr=$this->rewrite_rule; 
$s=$arr[0]; 
$e=$arr[1]; 
$content=preg_replace($s,$e,$content); 
} 
ob_end_clean(); 
echo $content; 
} 
} 
$tpl = new template('skin',"default"); 
$tpl->objdir='tpp'; 
$tpl->rewrite=true;//开启rewrite 需要服务器支持 
$tpl->rewrite_rule=array(array("/index\.php/"),array("index.html")); //rewrite规则 
$tpl->assign("indexurl","index.php"); 
$tpl->assign("str","我是字符串啦啦啦"); 
$tpl->assign("ec","我是被echo出来的"); 
$tpl->assign("subhtml","{subtpl ttt}这是用来引入一个模板文件的,这个就是引入ttt.htm"); 
$tpl->assign("a",array('dasdasd'.'bbbbbbb','cccccccccccccc')); 
$tpl->assign("i",1); 
$tpl->display("index"); 
?>

新建 tpl/default/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>无标题文档</title> 
</head> <body> 
1.字符串赋值 :<br /> 
{$str} 
<br /> 
2.数组赋值 :<br /> 
{loop $a $v}{$v},{/loop} 
或者<br /> 
{loop $a $key $val }{$val},{/loop} 
3.{$subhtml}<br /> 
{subtpl ttt}<br /> 
4.原来我是{$indexurl } 现在我被变成了index.php<br /> 
5.我还可以 echo 出来呢<br /> 
{echo $ec}<br /> 
6.其实我还可以加减乘除的 6*7*8 
{echo 6*7*8;} 
7.常用的就这些了 还有什么不懂的 <br /> 
</body> 
</html>

新建 tpl/default/ttt.html
新建 tpp目录 ok了
PHP 相关文章推荐
PHP和XSS跨站攻击的防范
Apr 17 PHP
php将fileterms函数返回的结果变成可读的形式
Apr 21 PHP
PHP中设置时区,记录日志文件的实现代码
Jan 07 PHP
PHP 登录记住密码实现思路
May 07 PHP
Php连接及读取和写入mysql数据库的常用代码
Aug 11 PHP
PHP实现数组递归转义的方法
Aug 28 PHP
php实现网页缓存的工具类分享
Jul 14 PHP
基于PHP生成简单的验证码
Jun 01 PHP
总结PHP中DateTime的常用方法
Aug 11 PHP
PHP实现判断数组是一维、二维或几维的方法
Feb 06 PHP
浅谈Laravel模板实体转义带来的坑
Oct 22 PHP
YII2框架中ActiveDataProvider与GridView的配合使用操作示例
Mar 18 PHP
php skymvc 一款轻量、简单的php
Jun 28 #PHP
关于php mvc开发模式的感想
Jun 28 #PHP
yii框架源码分析之创建controller代码
Jun 28 #PHP
关于PHP中Object对象的笔记分享
Jun 28 #PHP
php dirname(__FILE__) 获取当前文件的绝对路径
Jun 28 #PHP
php新建文件自动编号的思路与实现
Jun 27 #PHP
调整优化您的LAMP应用程序的5种简单方法
Jun 26 #PHP
You might like
PHP4实际应用经验篇(8)
2006/10/09 PHP
php stream_get_meta_data返回值
2013/09/29 PHP
jQuery实现表单input中提示文字value随鼠标焦点移进移出而显示或隐藏的代码
2010/03/21 Javascript
JavaScript中关于indexOf的使用方法与问题小结
2010/08/05 Javascript
通过javascript获取iframe里的值示例代码
2013/06/24 Javascript
多种方法判断Javascript对象是否存在
2013/09/22 Javascript
JS实现自动切换文字的导航效果代码
2015/08/27 Javascript
jQuery使用ajax跨域获取数据的简单实例
2016/05/18 Javascript
微信JS-SDK坐标位置如何转换为百度地图坐标
2016/07/04 Javascript
酷! 不同风格页面布局幻灯片特效js实现
2021/02/19 Javascript
JQuery 动态生成Table表格实例代码
2016/12/02 Javascript
基于Vue的文字跑马灯组件(npm 组件包)
2017/05/24 Javascript
用vscode开发vue应用的方法步骤
2019/05/06 Javascript
webpack-mvc 传统多页面组件化开发详解
2019/05/07 Javascript
深入解析微信小程序开发中遇到的几个小问题
2020/07/11 Javascript
[01:11:08]Winstrike vs NB 2018国际邀请赛淘汰赛BO1 8.21
2018/08/22 DOTA
Python批量按比例缩小图片脚本分享
2015/05/21 Python
python3中dict(字典)的使用方法示例
2017/03/22 Python
Python算术运算符实例详解
2017/05/31 Python
Python如何筛选序列中的元素的方法实现
2019/07/15 Python
详解将Python程序(.py)转换为Windows可执行文件(.exe)
2019/07/19 Python
TensorFlow 输出checkpoint 中的变量名与变量值方式
2020/02/11 Python
Python函数默认参数常见问题及解决方案
2020/03/26 Python
OpenCV 之按位运算举例解析
2020/06/19 Python
Python基于pyjnius库实现访问java类
2020/07/31 Python
Html5页面二次分享的实现
2018/07/30 HTML / CSS
打架检讨书100字
2014/01/08 职场文书
分公司负责人任命书
2014/06/04 职场文书
简单的个人租房协议书范本
2014/11/26 职场文书
2015年幼儿园教研活动总结
2015/03/25 职场文书
部队2015年终工作总结
2015/04/02 职场文书
2019年预备党员的思想汇报:加深对党的认知
2019/09/25 职场文书
python获取淘宝服务器时间的代码示例
2021/04/22 Python
一次SQL如何查重及去重的实战记录
2022/03/13 MySQL
《总之就是很可爱》新作短篇动画《总之就是很可爱~制服~》将于2022年夏天播出
2022/04/07 日漫
使用Nginx的访问日志统计PV与UV
2022/05/06 Servers