include/template.func.php在哪,discuzx模板解析原理template.func.php文件分析

discuzx模板解析原理template.func.php文件分析废话不说看原文吧!

./include/template.func.php

本帖不仅仅是分析文件,还把Discuz中模板解析这一原理分析了一下。

是用正则表达式替换一些模板中的规定的语言标记,然后呢,写到forumdata/templates中,再用include引用到index,forumdisplay等等中,和smarty的原理基本上相同,只是功能没有smarty那么多,smarty内建了一个cache来着…连个User Guide都几百页…

呵呵,不过现在基本上两个都没用过,正则表达式好是好用,不过正则的效率不是很高,以前看PHP技术文档的时候说能不用正则就尽量不要用,那东西烦着,

现在做什么项目基本上用的是框架,MVC的模式,View中的代码一般不用模板解析出来,混杂php代码在里面,也觉得不错,OOP的开发效率比较高,很多地方重用

代码是很开心的~!

Discuz的模板解析要分析出来只要用到两个文件:./include/global.func.php和./include/template.func.php,global只要一个函数就够了,

template要全部的文件下面分开讲一下,会比较详细,大家耐心看:

Section One--./include/global.func.php---->template function

代码:function template($file, $templateid = 0, $tpldir = '') {

global $tplrefresh;

$tpldir = $tpldir ? $tpldir : TPLDIR;

$templateid = $templateid ? $templateid : TEMPLATEID;

$tplfile = DISCUZ_ROOT.'./'.$tpldir.'/'.$file.'.htm';

$objfile = DISCUZ_ROOT.'./forumdata/templates/'.$templateid.'_'.$file.'.tpl.php';

if(TEMPLATEID != 1 && $templateid != 1 && !file_exists($tplfile)) {

return template($file, 1, './templates/default/');

}

if($tplrefresh == 1 || ($tplrefresh > 1 && substr($GLOBALS['timestamp'], -1) > $tplrefresh)) {

if(@filemtime($tplfile) > @filemtime($objfile)) {

require_once DISCUZ_ROOT.'./include/template.func.php';

parse_template($file, $templateid, $tpldir);

}

}

return $objfile;

}

这个函数一共有三个传入参数:

$file 表示模板名

$templateid 表示模板id

$tpldir 表示模板目录引用:

global $tplrefresh;

这个是把$tplrefresh作为一个全局变量,tplrefresh表示是不是刷新模板引用:

$tpldir = $tpldir ? $tpldir : TPLDIR;

$templateid = $templateid ? $templateid : TEMPLATEID;

给$tpldir和$templateid赋值,如果没有传进来就用常量TPLDIR和TEMPLATEID给它们值引用:

$tplfile = DISCUZ_ROOT.'./'.$tpldir.'/'.$file.'.htm';

$objfile = DISCUZ_ROOT.'./forumdata/templates/'.$templateid.'_'.$file.'.tpl.php';

这里是把$tplfile(表示的是模板文件)和$objfile(表示的是要编译成的文件)赋值引用:

if(TEMPLATEID != 1 && $templateid != 1 && !file_exists($tplfile)) {

return template($file, 1, './templates/default/');

}

防止TEMPLATEID不等于1且$templateid不等于1,而且模板文件不存在导致空白问题。

这里也可以算一个迭代,也可以不算,就是把1作为第二个参数再调用函数本身。引用:

if($tplrefresh == 1 || ($tplrefresh > 1 && substr($GLOBALS['timestamp'], -1) > $tplrefresh)) {

if(@filemtime($tplfile) > @filemtime($objfile)) {

require_once DISCUZ_ROOT.'./include/template.func.php';

parse_template($file, $templateid, $tpldir);

}

}

return $objfile;

很巧妙的一段,Discuz的模板缓存就体现在这里,如果你没打开模板刷新的话(config.inc.php->$tplrefresh=0),

这里就直接返回一个$objfile了,而这个文件是你第一次建论坛的时候就写入的,如果你不改模板的话,

关了是能提高相当一部分效率的!反之,如果你打开了模板刷新的话就接着判断是不是模板文件的建立时间大于forumdata/templates下的文件,

是的话就引用./include/template.func.php写入模板文件到forumdata/templates中,否则的话还是直接返回一个已经编译好的模板文件。

关于template.func.php文件中函数的分析在下面:

代码://防止非法引用

if(!defined('IN_DISCUZ')) {

exit('Access Denied');

}代码:/**

* 这个是关键模板解析函数,通过正则表达式来替换掉模板中的相关语句,使之变成标准的php语句,写入缓存(./forumdata/template),从而include到页面中显示出来

* @para string $file //解析的模板名,只要文件名就可以了,会自动加上htm,如果有缓存的话就变成"模板id_文件名.tpl.php"

* @para int $templateid //模板的id

* @para string $tpldir //模板所在的目录

*

*/

function parse_template($file, $templateid, $tpldir) {

global $language;

$nest = 5;

$tplfile = DISCUZ_ROOT."./$tpldir/$file.htm";

$objfile = DISCUZ_ROOT."./forumdata/templates/{$templateid}_$file.tpl.php";

if([email protected]$fp = fopen($tplfile, 'r')) {

dexit("Current template file './$tpldir/$file.htm' not found or have no access!");

} elseif(!include_once language('templates', $templateid, $tpldir)) {

dexit("

Current template pack do not have a necessary language file 'templates.lang.php' or have syntax error!");

}

$template = fread($fp, filesize($tplfile));

fclose($fp);

$var_regexp = "(($[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)([[a-zA-Z0-9_-."'[]$x7f-xff]+])*)";

$const_regexp = "([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)";

$template = preg_replace("/([nr]+)t+/s", "1", $template);

$template = preg_replace("//s", "{1}", $template);

$template = preg_replace("/{langs+(.+?)}/ies", "languagevar('1')", $template);

$template = preg_replace("/{faqs+(.+?)}/ies", "faqvar('1')", $template);

$template = str_replace("{LF}", "", $template);

$template = preg_replace("/{($[a-zA-Z0-9_[]'"$.x7f-xff]+)}/s", "", $template);

$template = preg_replace("/$var_regexp/es", "addquote('')", $template);

$template = preg_replace("/?>/es", "addquote('')", $template);

$template = "n$template";

$template =preg_replace("/[nrt]*{templates+([a-z0-9_]+)}[nrt]*/is","nn", $template);

$template =preg_replace("/[nrt]*{templates+(.+?)}[nrt]*/is", "nn", $template);

$template = preg_replace("/[nrt]*{evals+(.+?)}[nrt]*/ies", "stripvtags('nn','')", $template);

$template =preg_replace("/[nrt]*{echos+(.+?)}[nrt]*/ies","stripvtags('nn','')", $template);

$template =preg_replace("/[nrt]*{elseifs+(.+?)}[nrt]*/ies","stripvtags('nn','')", $template);

$template = preg_replace("/[nrt]*{else}[nrt]*/is", "nn", $template);

for($i = 0; $i < $nest; $i++) {

$template =preg_replace("/[nrt]*{loops+(S+)s+(S+)}[nr]*(.+?)[nr]*{/loop}[nrt]*/ies", "stripvtags('n','n3nn')", $template);

$template =preg_replace("/[nrt]*{loops+(S+)s+(S+)s+(S+)}[nrt]*(.+?)[nrt]*{/loop}[nrt]*/ies", "stripvtags('n','n4nn')", $template);

$template =preg_replace("/[nrt]*{ifs+(.+?)}[nr]*(.+?)[nr]*{/if}[nrt]*/ies", "stripvtags('n','n2nn')",$template);

}

$template = preg_replace("/{$const_regexp}/s", "", $template);

$template = preg_replace("/ ?>[nr]*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值