phpcms template函数分析

本文分析了一个名为template的函数,该函数用于加载和编译指定风格的模板文件。文章详细解释了函数如何处理不同类型的输入参数,并介绍了如何根据模板文件的存在情况及修改时间来决定是否重新编译模板。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/////////////////////////////////////
// template(模板调用)函数分析
//
/////////////////////////////////////
function template 位于 libs/functions/globel.func.php

一. 参数分析
参数($module 
$template $style)
 
   模块名    模板名    模板风格

 
  下面会以$module='content',$template='index',$style=''为例

二. 代码分段分析
1.  if(strpos($module, 'plugin/')!== false){
      $plugin = str_replace('plugin/', '',$module);
      return p_template($plugin,$template,$style);
    }
    //检测是否是插件调用如果是调用p_template()函数处理(分析在p_template函数分析.txt)
-------------------------------------------------------------------------------------------------

2.  $module = str_replace('/',DIRECTORY_SEPARATOR, $module);
   //把$module中原本的'/'替换成系统分隔符
    
-------------------------------------------------------------------------------------------------

3.  if(!empty($style) &&preg_match('/([a-z0-9\-_]+)/is',$style)) {
    } elseif(empty($style) && !defined('STYLE')) {
      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';
    }
    如果$style不为空$style没有特殊字符
 
     如果$style为空 和 未定义STYLE常量
 
        得到$siteid(站点ID)
 
        通过sitelist缓存得到$style
 
     如果$style为空 和 已定义STYLE常量
 
        $style = STYLE;
 
     其他状况 $style ='default';(采用默认)      
-------------------------------------------------------------------------------------------------

4.  if(!$style) $style ='default';  //如果$style为空 $style用默认风格(那上面在干嘛?)

-------------------------------------------------------------------------------------------------

5.  $template_cache =pc_base::load_sys_class('template_cache'); //实例化template_cache类

-------------------------------------------------------------------------------------------------

6.  $compiledtplfile =PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';
    //得到缓存路径,以$module='content',$template='index'为例, 实际字符串(路径)为D:\xampp\htdos\www\caches\caches_template\defalut\content\index.php
    
-------------------------------------------------------------------------------------------------

7.   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{
      $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 notexist.'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html');
      }
    }
    如果有D:\xampp\htdos\www\phpcms\defalut\templates\content\index.html(模板文件)
 
     如果 $compiledtplfile缓存文件php不存在 或者模板文件html的修改时间>缓存文件php的修改时间
 
        重新编译模板文件, 把v9的标签语言编译成php语言,放入对应风格缓存文件中(详细过程分析在template_cache类分析.txt)
 
    其他 (模板文件不存在)
 
     $compiledtplfile重新得到缓存文件路径, 实际字符串(路径)为D:\xampp\htdos\www\caches\caches_template\defalut\content\index.php(针对其他模板风格,如果没有其中对应的模板文件html,则使用默认风格中的对应缓存文件php路径)
 
     如果 $compiledtplfile缓存文件php不存在 或者 (默认风格存在对应模板html并且 默认风格对应模板html修改时间>默认风格对应缓存文件php修改时间)
 
        重新编译模板文件, 把v9的标签语言编译成php语言,放入默认风格缓存文件中(详细过程分析在template_cache类分析.txt)
 
     又如果 默认风格都不存在对应模板html
 
        弹出警告 : 模板不存在
    
-------------------------------------------------------------------------------------------------      
      
8.    return$compiledtplfile; //返回缓存文件绝对路径; 提供给include();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值