本帖最后由 shitou126 于 2012-3-21 10:11 编辑
对template函数的解析,有些地方没有详细注释,希望大家对这个函数的解析完善- php文件使用模板include_once
- template("diy:portal/view:{$catid}", NULL, NULL, NULL, ,
- $cat['articleprimaltplname']);"E:\wamp\www\mydz0.com\./data/template/1_diy_portal_view.tpl.php"$cat['articleprimaltplname']);$cat['articleprimaltplname'] "portal/view"$catid
- "135"$cat['articleprimaltplname'] "portal/view_pm_3"
- function template($file, $templateid = 0, $tpldir = '', $gettplfile = 0, $primaltpl='') {
- global $_G;
- static $_init_style = false;
- if($_init_style === false) {
- $discuz = & discuz_core::instance();
- $discuz->_init_style();//初始化模板风格
- $_init_style = true;
- }
- $oldfile = $file;
- if(strpos($file, ':') !== false) {
- $clonefile = '';
- list($templateid, $file, $clonefile) = explode(':', $file);
- //diy portal/view 135
- $oldfile = $file; //$oldfile = portal/view
- $file = empty($clonefile) || STYLEID != $_G['cache']['style_default']['styleid'] ? $file : $file.'_'.$clonefile;
- //$file = portal_view_135
- if($templateid == 'diy' && STYLEID == $_G['cache']['style_default']['styleid']) {
- $_G['style']['prefile'] = '';
- $diypath = DISCUZ_ROOT.'./data/diy/'; //DIY模板文件目录
- $preend = '_diy_preview';
- $_G['gp_preview'] = !empty($_G['gp_preview']) ? $_G['gp_preview'] : '';
- $curtplname = $oldfile;
- if(isset($_G['cache']['diytemplatename'.$_G['basescript']])) {
- $diytemplatename = &$_G['cache']['diytemplatename'.$_G['basescript']];
- } else {
- $diytemplatename = &$_G['cache']['diytemplatename'];
- }
- {如果满足以下条件,则$tpldir = ‘data/diy’
- 否则 $file = $primaltpl? $primaltpl: $oldfile;
- $_G['cache']['style_default']['styleid']
- : 1$_G['gp_preview'] : 0$curtplname =
- $oldfile; $_G['basescript'] : portal
- $_G['cache']['diytemplatename'.$_G['basescript']] :
- ["portal/view_130"]=> bool(true) ["portal/view_135"]=> bool(true) ["portal/list_2"]=> bool(true)
- ......}
- $tplsavemod = 0;
-
- if(isset($diytemplatename[$file]) &&
- file_exists($diypath.$file.'.htm') && ($tplsavemod= 1) || ($file
- = $primaltpl ? $primaltpl : $oldfile) &&
- isset($diytemplatename[$file]) &&
- file_exists($diypath.$file.'.htm')) {
- $tpldir = 'data/diy';
- !$gettplfile && $_G['style']['tplsavemod'] = $tplsavemod;
- $curtplname = $file;
- if($_G['gp_diy'] == 'yes' || $_G['gp_preview'] == 'yes') { //DIY模式或预览模式下做以下判断
- $flag = file_exists($diypath.$file.$preend.'.htm');
- if($_G['gp_preview'] == 'yes') {
- $file .= $flag ? $preend : '';
- } else {
- $_G['style']['prefile'] = $flag ? 1 : '';
- }
- }
- } else {
- $file = $primaltpl ? $primaltpl : $oldfile;
- //$file = portal/view_pm_3.htm;
- }
- $tplrefresh = $_G['config']['output']['tplrefresh'];
-
- if($tpldir == 'data/diy' && ($tplrefresh==1 ||
- ($tplrefresh > 1 && !($_G['timestamp'] % $tplrefresh)))
- && filemtime($diypath.$file.'.htm') <
- filemtime(DISCUZ_ROOT.TPLDIR.'/'.($primaltpl ? $primaltpl :
- $oldfile).'.htm')) {
- if (!updatediytemplate($file)) {
- unlink($diypath.$file.'.htm');
- $tpldir = '';
- }
- }
- if (!$gettplfile && empty($_G['style']['tplfile'])) {
- $_G['style']['tplfile'] = empty($clonefile) ? $curtplname : $oldfile.':'.$clonefile;
- }
-
- $_G['style']['prefile'] = !empty($_G['gp_preview']) &&
- $_G['gp_preview'] == 'yes' ? '' : $_G['style']['prefile'];
- } else {
- $tpldir = './source/plugin/'.$templateid.'/template';
- //如果$templateid不为diy,则为插件模板目录
- }
- }
- //如果是ajax请求
- $file .= !empty($_G['inajax']) && ($file == 'common/header' || $file == 'common/footer') ? '_ajax' : '';
- //如果是ajax请求,则common/header 变为 common/header_ajax
- {
- ./static/common.js 中封装的ajax请求中 processHandle
- if(aj.recvType == 'HTML') {
- aj.resultHandle(aj.XMLHttpRequest.responseText,aj);
- } else if(aj.recvType == 'XML') {
-
-
- if(!aj.XMLHttpRequest.responseXML
- ||!aj.XMLHttpRequest.responseXML.lastChild
- ||aj.XMLHttpRequest.responseXML.lastChild.localName == 'parsererror') {
-
- aj.resultHandle('<ahref="' + aj.targetUrl + '"
- target="_blank"style="color:red">内部错误,无法显示此内容</a>' , aj);
- } else {
- aj.resultHandle(aj.XMLHttpRequest.responseXML.lastChild.firstChild.nodeValue,aj);
- }
- }
- }
- $tpldir = $tpldir ? $tpldir : (defined('TPLDIR') ? TPLDIR : '');
- // 模板路径
- $templateid = $templateid ? $templateid : (defined('TEMPLATEID') ? TEMPLATEID : '');
- $filebak = $file;
- //$filebak = $file;如果STYLEID 不是默认的id,则$filebak 为:portal/view
-
- if(defined('IN_MOBILE') &&!defined('TPL_DEFAULT') &&
- strpos($file, 'mobile/') === false || $_G['forcemobilemessage']) {
- $file = 'mobile/'.$oldfile;
- }
- $tplfile = ($tpldir ? $tpldir.'/' : './template/').$file.'.htm';
- //./template/default/portal/view_pm_3.htm
-
- $file == 'common/header' && defined('CURMODULE') &&
- CURMODULE && $file =
- 'common/header_'.$_G['basescript'].'_'.CURMODULE;
- if(defined('IN_MOBILE') &&!defined('TPL_DEFAULT')) {
- if(strpos($tpldir, 'plugin')) {
- if(!file_exists(DISCUZ_ROOT.$tpldir.'/'.$file.'.htm')) {
- require_once libfile('class/error');
- discuz_error::template_error('template_notfound', $tpldir.'/'.$file.'.htm');
- } else {
- $mobiletplfile = $tpldir.'/'.$file.'.htm';
- }
- }
- !$mobiletplfile && $mobiletplfile = $file.'.htm';
- if(strpos($tpldir, 'plugin') && file_exists(DISCUZ_ROOT.$mobiletplfile)) {
- $tplfile = $mobiletplfile;
- } elseif(!file_exists(DISCUZ_ROOT.TPLDIR.'/'.$mobiletplfile)) {
- $mobiletplfile = './template/default/'.$mobiletplfile;
- if(!file_exists(DISCUZ_ROOT.$mobiletplfile) && !$_G['forcemobilemessage']) {
- $tplfile = str_replace('mobile/', '', $tplfile);
- $file = str_replace('mobile/', '', $file);
- define('TPL_DEFAULT', true);
- } else {
- $tplfile = $mobiletplfile;
- }
- } else {
- $tplfile = TPLDIR.'/'.$mobiletplfile;
- }
- }
-
- $cachefile = './data/template/'.(defined('STYLEID') ? STYLEID.'_' :
- '_').$templateid.'_'.str_replace('/', '_', $file).'.tpl.php';
- //./data/template/4_diy_portal_view.tpl.php
- if($templateid != 1 &&!file_exists(DISCUZ_ROOT.$tplfile)) {
- $tplfile = './template/default/'.$filebak.'.htm';
- //./template/default/portal/view.htm
- }
- if($gettplfile) {
- return $tplfile;
- }
- checktplrefresh($tplfile, $tplfile, @filemtime(DISCUZ_ROOT.$cachefile), $templateid, $cachefile, $tpldir, $file);
- return DISCUZ_ROOT.$cachefile;
- }
复制代码 |