phpcms源码解析之模板(template)函数

模板调用函数在global.func.php中。
<?php

/**
 * 模板调用
 *
 * @param $module
 * @param $template
 * @param $istag
 * @return unknown_type
 */
function template($module = 'content', $template = 'index', $style = '') {

 //插件处理
 if(strpos($module, 'plugin/')!== false) {    //插件检测 , 包含plugin/的为插件
  $plugin = str_replace('plugin/', '', $module);//将模板中plugin/替换,去除
  return p_template($plugin, $template,$style); //调用插件前台模板函数
 }
 $module = str_replace('/', DIRECTORY_SEPARATOR, $module);
 
 //style设置, 默认default
 if(!empty($style) && preg_match('/([a-z0-9\-_]+)/is',$style)) {
 } elseif (empty($style) && !defined('STYLE')) {
  
  //设置siteid
  if(defined('SITEID')) {
   $siteid = SITEID;
  } else {
   $siteid = param::get_cookie('siteid');
  }
  if (!$siteid) $siteid = 1;
  $sitelist = getcache('sitelist','commons');
  if(!empty($siteid)) {
   $style = $sitelist[$siteid]['default_style'];
  }
  
 } elseif (empty($style) && defined('STYLE')) {
  $style = STYLE;
 } else {
  $style = 'default';
 }
 if(!$style) $style = 'default';
 
 
 $template_cache = pc_base::load_sys_class('template_cache');//加载模板缓冲类template_cache
 
 //
 /*
  * 编译模板缓冲文件
  * PHPCMS_PATH="/" 
  * /caches/caches_template/defaults/$module/$template.php
  * */ 
 $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';

 /*
  * 模板文件
  * PC_PATH='phpcms/'
  * phpcms/templates/defaults/$module/$template.html
  * */
 if(file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {
  //检测模板缓存文件
  if(!file_exists($compiledtplfile) || (@filemtime(PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') > @filemtime($compiledtplfile))) {
   //若模板缓存文件不存在,或是模板文件更新,则执行下面代码,对模板文件进行编译
   $template_cache->template_compile($module, $template, $style);
  }
 } else { //执行style=default
  $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';
  if(!file_exists($compiledtplfile) || (file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') && filemtime(PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') > filemtime($compiledtplfile))) {
   $template_cache->template_compile($module, $template, 'default');
   //模板文件不存在
  } elseif (!file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {
   showmessage('Template does not exist.'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html');
  }
 }
 return $compiledtplfile;  //返回最新的编译缓存文件
}


/**
 * 插件前台模板加载
 * Enter description here ...
 * @param unknown_type $module
 * @param unknown_type $template
 * @param unknown_type $style
 */
function p_template($plugin = 'content', $template = 'index',$style='default') {
 if(!$style) $style = 'default';
 $template_cache = pc_base::load_sys_class('template_cache');
 $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.$template.'.php';
 
 /*
  * phpcms/plugin/$plugin/templates/$template.html
  * */
 if(!file_exists($compiledtplfile) || (file_exists(PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$template.'.html') && filemtime(PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$template.'.html') > filemtime($compiledtplfile))) {
  $template_cache->template_compile('plugin/'.$plugin, $template, 'default');
 } elseif (!file_exists(PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$template.'.html')) {
  showmessage('Template does not exist.'.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.$template.'.html');
 }

 return $compiledtplfile;
}

?>

这里有调用到模板编译函数,下节将奉上!!!敬请期待,,,

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值