Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

[已解决] dz function template 函数解析

[复制链接]
shitou126 发表于 2012-3-21 10:04:58 | 显示全部楼层 |阅读模式
本帖最后由 shitou126 于 2012-3-21 10:11 编辑

对template函数的解析,有些地方没有详细注释,希望大家对这个函数的解析完善
  1. php文件使用模板include_once
  2. template("diy:portal/view:{$catid}", NULL, NULL, NULL, ,
  3. $cat['articleprimaltplname']);"E:\wamp\www\mydz0.com\./data/template/1_diy_portal_view.tpl.php"$cat['articleprimaltplname']);$cat['articleprimaltplname']  "portal/view"$catid
  4.     "135"$cat['articleprimaltplname']    "portal/view_pm_3"
  5. function template($file, $templateid = 0, $tpldir = '', $gettplfile = 0, $primaltpl='') {
  6.     global $_G;
  7.     static $_init_style = false;
  8.     if($_init_style === false) {
  9.        $discuz = & discuz_core::instance();
  10.        $discuz->_init_style();//初始化模板风格
  11.        $_init_style = true;
  12.     }
  13.     $oldfile = $file;
  14.     if(strpos($file, ':') !== false) {
  15.        $clonefile = '';
  16.        list($templateid, $file, $clonefile) = explode(':', $file);
  17. //diy    portal/view     135
  18.        $oldfile = $file; //$oldfile = portal/view
  19.        $file = empty($clonefile) || STYLEID != $_G['cache']['style_default']['styleid'] ? $file : $file.'_'.$clonefile;
  20.            //$file = portal_view_135
  21.        if($templateid == 'diy' && STYLEID == $_G['cache']['style_default']['styleid']) {
  22.            $_G['style']['prefile'] = '';
  23.            $diypath = DISCUZ_ROOT.'./data/diy/'; //DIY模板文件目录
  24.            $preend = '_diy_preview';
  25.            $_G['gp_preview'] = !empty($_G['gp_preview']) ? $_G['gp_preview'] : '';
  26.            $curtplname = $oldfile;
  27.            if(isset($_G['cache']['diytemplatename'.$_G['basescript']])) {
  28.               $diytemplatename = &$_G['cache']['diytemplatename'.$_G['basescript']];
  29.            } else {
  30.               $diytemplatename = &$_G['cache']['diytemplatename'];
  31.            }
  32. {如果满足以下条件,则$tpldir = ‘data/diy’
  33. 否则 $file = $primaltpl? $primaltpl: $oldfile;
  34. $_G['cache']['style_default']['styleid']
  35.     :       1$_G['gp_preview']                    :       0$curtplname =
  36. $oldfile; $_G['basescript']                     :        portal         
  37. $_G['cache']['diytemplatename'.$_G['basescript']] :
  38. ["portal/view_130"]=>  bool(true)  ["portal/view_135"]=>  bool(true)  ["portal/list_2"]=>  bool(true)
  39. ......}
  40.            $tplsavemod = 0;
  41.         
  42.    if(isset($diytemplatename[$file]) &&
  43. file_exists($diypath.$file.'.htm') && ($tplsavemod= 1) || ($file
  44. = $primaltpl ? $primaltpl : $oldfile) &&
  45. isset($diytemplatename[$file]) &&
  46. file_exists($diypath.$file.'.htm')) {
  47.               $tpldir = 'data/diy';
  48.               !$gettplfile && $_G['style']['tplsavemod'] = $tplsavemod;
  49.               $curtplname = $file;
  50.               if($_G['gp_diy'] == 'yes' || $_G['gp_preview'] == 'yes') { //DIY模式或预览模式下做以下判断
  51.                   $flag = file_exists($diypath.$file.$preend.'.htm');
  52.                   if($_G['gp_preview'] == 'yes') {
  53.                      $file .= $flag ? $preend : '';
  54.                   } else {
  55.                      $_G['style']['prefile'] = $flag ? 1 : '';
  56.                   }
  57.               }
  58.            } else {
  59.               $file = $primaltpl ? $primaltpl : $oldfile;
  60. //$file = portal/view_pm_3.htm;
  61.            }
  62.            $tplrefresh = $_G['config']['output']['tplrefresh'];

  63.           if($tpldir == 'data/diy' && ($tplrefresh==1 ||
  64. ($tplrefresh > 1 && !($_G['timestamp'] % $tplrefresh)))
  65. && filemtime($diypath.$file.'.htm') <
  66. filemtime(DISCUZ_ROOT.TPLDIR.'/'.($primaltpl ? $primaltpl :
  67. $oldfile).'.htm')) {
  68.               if (!updatediytemplate($file)) {
  69.                   unlink($diypath.$file.'.htm');
  70.                   $tpldir = '';
  71.               }
  72.            }
  73.            if (!$gettplfile && empty($_G['style']['tplfile'])) {
  74.               $_G['style']['tplfile'] = empty($clonefile) ? $curtplname : $oldfile.':'.$clonefile;
  75.            }

  76.           $_G['style']['prefile'] = !empty($_G['gp_preview']) &&
  77. $_G['gp_preview'] == 'yes' ? '' : $_G['style']['prefile'];
  78.        } else {
  79.            $tpldir = './source/plugin/'.$templateid.'/template';
  80. //如果$templateid不为diy,则为插件模板目录
  81.        }
  82.     }
  83. //如果是ajax请求
  84.     $file .= !empty($_G['inajax']) && ($file == 'common/header' || $file == 'common/footer') ? '_ajax' : '';
  85. //如果是ajax请求,则common/header 变为 common/header_ajax
  86. {
  87. ./static/common.js 中封装的ajax请求中 processHandle
  88. if(aj.recvType == 'HTML') {
  89.               aj.resultHandle(aj.XMLHttpRequest.responseText,aj);
  90.            } else if(aj.recvType == 'XML') {
  91.               

  92.              if(!aj.XMLHttpRequest.responseXML
  93. ||!aj.XMLHttpRequest.responseXML.lastChild
  94. ||aj.XMLHttpRequest.responseXML.lastChild.localName == 'parsererror') {

  95.                  aj.resultHandle('<ahref="' + aj.targetUrl + '"
  96. target="_blank"style="color:red">内部错误,无法显示此内容</a>' , aj);
  97.               } else {
  98.                  aj.resultHandle(aj.XMLHttpRequest.responseXML.lastChild.firstChild.nodeValue,aj);
  99.               }
  100.            }
  101. }
  102.     $tpldir = $tpldir ? $tpldir : (defined('TPLDIR') ? TPLDIR : '');
  103. // 模板路径
  104.     $templateid = $templateid ? $templateid : (defined('TEMPLATEID') ? TEMPLATEID : '');
  105.     $filebak = $file;
  106. //$filebak = $file;如果STYLEID 不是默认的id,则$filebak 为:portal/view

  107.    if(defined('IN_MOBILE') &&!defined('TPL_DEFAULT') &&
  108. strpos($file, 'mobile/') === false || $_G['forcemobilemessage']) {
  109.        $file = 'mobile/'.$oldfile;
  110.     }
  111.     $tplfile = ($tpldir ? $tpldir.'/' : './template/').$file.'.htm';
  112. //./template/default/portal/view_pm_3.htm

  113.    $file == 'common/header' && defined('CURMODULE') &&
  114. CURMODULE && $file =
  115. 'common/header_'.$_G['basescript'].'_'.CURMODULE;
  116.     if(defined('IN_MOBILE') &&!defined('TPL_DEFAULT')) {
  117.        if(strpos($tpldir, 'plugin')) {
  118.            if(!file_exists(DISCUZ_ROOT.$tpldir.'/'.$file.'.htm')) {
  119.               require_once libfile('class/error');
  120.               discuz_error::template_error('template_notfound', $tpldir.'/'.$file.'.htm');
  121.            } else {
  122.               $mobiletplfile = $tpldir.'/'.$file.'.htm';
  123.            }
  124.        }
  125.        !$mobiletplfile && $mobiletplfile = $file.'.htm';
  126.        if(strpos($tpldir, 'plugin') && file_exists(DISCUZ_ROOT.$mobiletplfile)) {
  127.            $tplfile = $mobiletplfile;
  128.        } elseif(!file_exists(DISCUZ_ROOT.TPLDIR.'/'.$mobiletplfile)) {
  129.            $mobiletplfile = './template/default/'.$mobiletplfile;
  130.            if(!file_exists(DISCUZ_ROOT.$mobiletplfile) && !$_G['forcemobilemessage']) {
  131.               $tplfile = str_replace('mobile/', '', $tplfile);
  132.               $file = str_replace('mobile/', '', $file);
  133.               define('TPL_DEFAULT', true);
  134.            } else {
  135.               $tplfile = $mobiletplfile;
  136.            }
  137.        } else {
  138.            $tplfile = TPLDIR.'/'.$mobiletplfile;
  139.        }
  140.     }

  141.    $cachefile = './data/template/'.(defined('STYLEID') ? STYLEID.'_' :
  142. '_').$templateid.'_'.str_replace('/', '_', $file).'.tpl.php';
  143. //./data/template/4_diy_portal_view.tpl.php
  144.     if($templateid != 1 &&!file_exists(DISCUZ_ROOT.$tplfile)) {
  145.        $tplfile = './template/default/'.$filebak.'.htm';
  146.        //./template/default/portal/view.htm
  147.     }
  148.     if($gettplfile) {
  149.        return $tplfile;
  150.     }
  151.     checktplrefresh($tplfile, $tplfile, @filemtime(DISCUZ_ROOT.$cachefile), $templateid, $cachefile, $tpldir, $file);
  152.     return DISCUZ_ROOT.$cachefile;
  153. }
复制代码
evenzhou 发表于 2012-3-21 16:26:35 | 显示全部楼层
  1. /**
  2. * 解析模板
  3. * @return 返回域名
  4. */
  5. function template($file, $templateid = 0, $tpldir = '', $gettplfile = 0, $primaltpl='') {
  6.         global $_G;

  7.         static $_init_style = false;
  8.         if($_init_style === false) {
  9.                 $discuz = & discuz_core::instance();
  10.                 $discuz->_init_style();
  11.                 $_init_style = true;
  12.         }
  13.         $oldfile = $file; //原模板
  14.         if(strpos($file, ':') !== false) {
  15.                 $clonefile = '';
  16.                 list($templateid, $file, $clonefile) = explode(':', $file);
  17.                 $oldfile = $file;
  18.                 $file = empty($clonefile) || STYLEID != $_G['cache']['style_default']['styleid'] ? $file : $file.'_'.$clonefile;
  19.                 if($templateid == 'diy' && STYLEID == $_G['cache']['style_default']['styleid']) {
  20.                         $_G['style']['prefile'] = ''; //非预览环境标记预览文件是否存在
  21.                         $diypath = DISCUZ_ROOT.'./data/diy/'; //DIY模板文件目录
  22.                         $preend = '_diy_preview'; //预览文件后缀
  23.                         $_G['gp_preview'] = !empty($_G['gp_preview']) ? $_G['gp_preview'] : ''; //是否预览
  24.                         $curtplname = $oldfile;//当前模板名
  25.                         if(isset($_G['cache']['diytemplatename'.$_G['basescript']])) {
  26.                                 $diytemplatename = &$_G['cache']['diytemplatename'.$_G['basescript']];//当前应用的DIY文件缓存
  27.                         } else {
  28.                                 $diytemplatename = &$_G['cache']['diytemplatename'];//所有DIY文件缓存
  29.                         }
  30.                         $tplsavemod = 0; //公共DIY页面标记
  31.                         //独立DIY页面 || 公共DIY页面
  32.                         if(isset($diytemplatename[$file]) && file_exists($diypath.$file.'.htm') && ($tplsavemod = 1) || ($file = $primaltpl ? $primaltpl : $oldfile) && isset($diytemplatename[$file]) && file_exists($diypath.$file.'.htm')) {
  33.                                 $tpldir = 'data/diy'; //文件目录
  34.                                 !$gettplfile && $_G['style']['tplsavemod'] = $tplsavemod; //独立DIY页面标记:1,公共DIY页面标记:0
  35.                                 $curtplname = $file; //当前模板名
  36.                                 if($_G['gp_diy'] == 'yes' || $_G['gp_preview'] == 'yes') { //DIY模式或预览模式下做以下判断
  37.                                         $flag = file_exists($diypath.$file.$preend.'.htm'); //预览文件是否存在
  38.                                         if($_G['gp_preview'] == 'yes') { //预览环境
  39.                                                 $file .= $flag ? $preend : ''; //使用预览模板文件
  40.                                         } else {
  41.                                                 $_G['style']['prefile'] = $flag ? 1 : ''; //非预览环境标记预览文件是否存在
  42.                                         }
  43.                                 }
  44.                         } else {
  45.                                 $file = $primaltpl ? $primaltpl : $oldfile; //无DIY页面则使用原模板
  46.                         }
  47.                         //根据模板自动刷新开关$tplrefresh 更新DIY模板
  48.                         $tplrefresh = $_G['config']['output']['tplrefresh'];
  49.                         //在有DIY生成模板文件时 && 自动刷新开启 && DIY生成模板文件修改时间 < 原模板修改修改
  50.                         if($tpldir == 'data/diy' && ($tplrefresh ==1 || ($tplrefresh > 1 && !($_G['timestamp'] % $tplrefresh))) && filemtime($diypath.$file.'.htm') < filemtime(DISCUZ_ROOT.TPLDIR.'/'.($primaltpl ? $primaltpl : $oldfile).'.htm')) {
  51.                                 //原模板更改则更新DIY模板,如果更新失败则删除DIY模板
  52.                                 if (!updatediytemplate($file)) {
  53.                                         unlink($diypath.$file.'.htm');
  54.                                         $tpldir = '';
  55.                                 }
  56.                         }

  57.                         //保存当前模板名
  58.                         if (!$gettplfile && empty($_G['style']['tplfile'])) {
  59.                                 $_G['style']['tplfile'] = empty($clonefile) ? $curtplname : $oldfile.':'.$clonefile;
  60.                         }

  61.                         //是否显示继续DIY
  62.                         $_G['style']['prefile'] = !empty($_G['gp_preview']) && $_G['gp_preview'] == 'yes' ? '' : $_G['style']['prefile'];

  63.                 } else {
  64.                         $tpldir = './source/plugin/'.$templateid.'/template';
  65.                 }
  66.         }

  67.         //noteX 将页面模板加一层Mobile目录,用以定位手机模板页面(IN_MOBILE)
  68.         if(defined('IN_MOBILE') && !defined('TPL_DEFAULT') && strpos($file, 'mobile/') === false || $_G['forcemobilemessage']) {
  69.                 $file = 'mobile/'.$oldfile;
  70.         }

  71.         $file .= !empty($_G['inajax']) && ($file == 'common/header' || $file == 'common/footer') ? '_ajax' : '';
  72.         $tpldir = $tpldir ? $tpldir : (defined('TPLDIR') ? TPLDIR : '');
  73.         $templateid = $templateid ? $templateid : (defined('TEMPLATEID') ? TEMPLATEID : '');

  74.         $tplfile = ($tpldir ? $tpldir.'/' : './template/').$file.'.htm';

  75.         $filebak = $file;
  76.         $file == 'common/header' && defined('CURMODULE') && CURMODULE && $file = 'common/header_'.$_G['basescript'].'_'.CURMODULE;

  77.         //noteX 手机模板的判断(IN_MOBILE)
  78.         if(defined('IN_MOBILE') && !defined('TPL_DEFAULT')) {
  79.                 //首先判断是否是DIY模板,如果是就删除可能存在的forumdisplay_1中的数字
  80.                 if(strpos($tpldir, 'plugin')) {
  81.                         if(!file_exists(DISCUZ_ROOT.$tpldir.'/'.$file.'.htm')) {
  82.                                 require_once libfile('class/error');
  83.                                 discuz_error::template_error('template_notfound', $tpldir.'/'.$file.'.htm');
  84.                         } else {
  85.                                 $mobiletplfile = $tpldir.'/'.$file.'.htm';
  86.                         }
  87.                 }
  88.                 !$mobiletplfile && $mobiletplfile = $file.'.htm';
  89.                 if(strpos($tpldir, 'plugin') && file_exists(DISCUZ_ROOT.$mobiletplfile)) {
  90.                         $tplfile = $mobiletplfile;
  91.                 } elseif(!file_exists(DISCUZ_ROOT.TPLDIR.'/'.$mobiletplfile)) {
  92.                         $mobiletplfile = './template/default/'.$mobiletplfile;
  93.                         if(!file_exists(DISCUZ_ROOT.$mobiletplfile) && !$_G['forcemobilemessage']) {
  94.                                 $tplfile = str_replace('mobile/', '', $tplfile);
  95.                                 $file = str_replace('mobile/', '', $file);
  96.                                 $cachefile = str_replace('mobile_', '', $cachefile);
  97.                                 define('TPL_DEFAULT', true);
  98.                         } else {
  99.                                 $tplfile = $mobiletplfile;
  100.                         }
  101.                 } else {
  102.                         $tplfile = TPLDIR.'/'.$mobiletplfile;
  103.                 }
  104.         }

  105.         $cachefile = './data/template/'.(defined('STYLEID') ? STYLEID.'_' : '_').$templateid.'_'.str_replace('/', '_', $file).'.tpl.php';

  106.         if($templateid != 1 && !file_exists(DISCUZ_ROOT.$tplfile)) {
  107.                 $tplfile = './template/default/'.$filebak.'.htm';
  108.         }

  109.         if($gettplfile) {
  110.                 return $tplfile;
  111.         }
  112.         checktplrefresh($tplfile, $tplfile, @filemtime(DISCUZ_ROOT.$cachefile), $templateid, $cachefile, $tpldir, $file);
  113.         return DISCUZ_ROOT.$cachefile;
  114. }
复制代码
这已经是最详细的了,自己看看吧

评分

1

查看全部评分

回复

使用道具 举报

 楼主| shitou126 发表于 2012-3-21 16:44:59 | 显示全部楼层
evenzhou 发表于 2012-3-21 16:26
这已经是最详细的了,自己看看吧

谢谢了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-9-28 11:12 , Processed in 0.195915 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表