复制代码 代码如下:
<?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了
相关推荐:
SEO抢权:如何在竞争激烈的市场中占得先机,正规网站建设口碑好
SEO走动:提升网站流量的关键一步,SEO数据监控宝宝推荐
SEO优化与SEM广告:提升品牌曝光与流量的双重利器,ai接回头
SEO是如何推动企业增长的关键力量,深圳罗湖网站建设设计
seo辅助词选什么,seo助手 ,各车企ai
seo需要学习什么语言,做seo需要懂什么技术 ,ai软件制作教程
SEO作用:提升网站流量与品牌曝光的秘密武器,美容网站联盟平台推广
SEO是什么意思?揭秘SEO的真正含义与重要性,公司推广网站询问d火18星来
为什么seo这么难,seo难嘛 ,ai宁中则
SEO观察:2025年搜索引擎优化的新趋势与机遇,seo2是什么状态
企业如何借助SEO咨询实现精准流量引爆,助力业绩提升,立刻推广的旅游线下营销
seo需要学些什么内容,学seo的基础 ,中国ai公司年收入
seo运营经理是什么,seo和运营的区别 ,皖妍ai宁慕晴o
AI人工智能:改变未来的科技革命
SEO自从上线后的演变与未来发展趋势,拼多多增加关键词排名
AI提炼主要内容:如何让信息更精准、高效、易懂,女军人ai
优化综合:引领高效发展的智慧之道,电影营销的推广方式
SEO技术如何通过优化提升网站流量与排名,四平网站优化公司
优化软件:让电脑性能焕然一新,提升效率的秘密武器,大理网站推广招聘信息最新
ChatGPT网页打不开?快来看看这些解决办法,轻松恢复正常访问!,ai金色包装
求一个AI软件,彻底改变你的工作与生活!
SEO做好,企业网站流量翻倍的关键,seo白帽技术有哪些
Chat启用后ESX连不上?解决方法一网打尽!,为什么AI续写这么离谱
优化提长:让企业效能提升的秘密武器,长沙网站建设创意
洗文章AI:让内容创作变得更智能、更高效
WPQQ-开启数字时代的智能连接新纪元,杭州网站推广厂家电话
常用AI工具,高效智能生活
SEO教研:数字营销新趋势,提升网站流量与转化率的关键,齐鲁证券网站建设
SEO关键词推广软件官网-助力企业实现高效精准的网络营销,圈圈ai
从零到一:网站历史的演变与未来趋势,肇庆市国外网站建设平台
Bing学术搜索结果不显示时间?如何解决这一问题,提升学术研究效率!,ai怎么参考线
什么是神马排名?让你的网站脱颖而出,轻松占据搜索引擎的C位!,乳山网站优化关键词排名
专业SEO助力企业在激烈市场竞争中脱颖而出,嘉兴海外网站推广价格
AI免费文章解读:智能写作新篇章,小店AI
ChatGPT中文版下载,开启智能对话新体验,婚纱ai男
什么是seo推广找行者SEO,seo推广效果怎么样 ,西宁ai万词霸屏系统
亚马逊seo信息是什么,亚马逊seo关键词优化软件 ,光谷ai
为什么做seo矩阵项目,为什么做seo矩阵项目不能做 ,怎么用ai写作
为什么“未备案域名”会成为互联网行业中的重要问题?,江干区seo优化价格
seo网络推广是什么,seo网络推广是什么意思 ,ai哪里注音
互联网快照:记录数字时代的每个瞬间,全网seo怎么优化内容
SEO代做:让你的企业轻松登顶搜索引擎,快速提升曝光率,seo 提高注册量
ChatGPT-深度学习与自然语言处理的革命性突破,金华ai视觉锁螺丝机
WPJVX:开启数字化未来的智慧平台,关键词排名技术咨询乐云seo
AI撰写大数据解决方案:开启智能数据时代的新篇章,ai头号公敌
ChatGPT为什么用不了了?背后的真相揭秘!,ai写作专家收费吗
优化标题:如何让你的文章更具吸引力与点击力,整站网站优化解决方案
AI一键生成文章,写作新境界
AI网页生成:轻松构建智能网站,提升品牌竞争力,杭州专业ai智能教育
文章AI生成标题:让创作更轻松,内容更精彩