从康盛产品(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调用时间格式的参数详解
Jun 06 PHP
Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)
Jun 14 PHP
PHP随机字符串生成代码(包括大小写字母)
Jun 24 PHP
PHP高级编程实例:编写守护进程
Sep 02 PHP
安装ImageMagick出现error while loading shared libraries的解决方法
Sep 23 PHP
php表单提交与$_POST实例分析
Jan 26 PHP
ThinkPHP自定义Redis处理SESSION的实现方法
May 16 PHP
Yii2中使用join、joinwith多表关联查询
Jun 30 PHP
浅谈Coreseek、Sphinx-for-chinaese、Sphinx+Scws的区别
Dec 15 PHP
PHP 获取指定地区的天气实例代码
Feb 08 PHP
php中Ioc(控制反转)和Di(依赖注入)
May 07 PHP
PHP Swoole异步MySQL客户端实现方法示例
Oct 24 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
php switch语句多个值匹配同一代码块应用示例
2014/07/29 PHP
Yii实现的多级联动下拉菜单
2016/07/13 PHP
PHP制作登录异常ip检测功能的实例代码
2016/11/16 PHP
tp5(thinkPHP5框架)使用DB实现批量删除功能示例
2019/05/28 PHP
JavaScript面向对象之体会[总结]
2008/11/13 Javascript
对setInterval在火狐和chrome切换标签产生奇怪的效果之探索,与解决方案!
2011/10/29 Javascript
原生js实现给指定元素的后面追加内容
2013/04/10 Javascript
JavaScript中使用arguments获得函数传参个数实例
2014/08/27 Javascript
javascript随机显示背景图片的方法
2015/06/18 Javascript
js点击返回跳转到指定页面实现过程
2020/08/20 Javascript
jQuery Mobile中的button按钮组件基础使用教程
2016/05/23 Javascript
javascript 使用正则test( )第一次是 true,第二次是false
2017/02/22 Javascript
vue学习笔记之vue1.0和vue2.0的区别介绍
2017/05/17 Javascript
详解如何使用PM2将Node.js的集群变得更加容易
2017/11/15 Javascript
vue基础之模板和过滤器用法实例分析
2019/03/12 Javascript
Python实现的检测网站挂马程序
2014/11/30 Python
Python中exit、return、sys.exit()等使用实例和区别
2015/05/28 Python
正确理解python中的关键字“with”与上下文管理器
2017/04/21 Python
利用Python求阴影部分的面积实例代码
2018/12/05 Python
将pandas.dataframe的数据写入到文件中的方法
2018/12/07 Python
Python3.6中Twisted模块安装的问题与解决
2019/04/15 Python
python django框架中使用FastDFS分布式文件系统的安装方法
2019/06/10 Python
pandas DataFrame的修改方法(值、列、索引)
2019/08/02 Python
树莓派4B+opencv4+python 打开摄像头的实现方法
2019/10/18 Python
Python类绑定方法及非绑定方法实例解析
2020/10/09 Python
CSS3实现闪烁动画效果的方法
2015/02/09 HTML / CSS
Booking.com美国:全球酒店预订网站
2017/04/18 全球购物
Skyscanner阿联酋:全球领先的旅游搜索平台
2017/11/25 全球购物
德国高端单身人士交友网站:ElitePartner
2018/12/02 全球购物
教师应聘自荐信范文
2014/03/14 职场文书
《沉香救母》教学反思
2014/04/19 职场文书
入党综合考察材料
2014/06/02 职场文书
法制演讲稿
2014/09/10 职场文书
财务会计个人原因辞职信
2019/06/21 职场文书
浅谈Redis的几个过期策略
2021/05/27 Redis
springboot临时文件存储目录配置方式
2021/07/01 Java/Android