phpcms template函数分析

来源:互联网 发布:手机单机飞机游戏 知乎 编辑:程序博客网 时间:2024/06/05 16:35
/////////////////////////////////////
// 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();
原创粉丝点击