日期:2014-05-17  浏览次数:20464 次

phpcms 源码解析-------模板引擎
PHPCMS 整站代码分析讲解(六)之模板引擎
发布:水水

19 May

<?php
/**
函数 template函数是在global.func.php 里面定义的。 在前面的phpcms 的首页 index.php 里就见到了。  用法: include template()    用法很熟, 呵呵其实和 dz 的模板引擎一样的用法。 但DZ的模板引擎比 PHPCMS 的简单很多,因为没有用到模板的标签技术。 大家有空可以研究下DZ的模板引擎。这里不说。  好分析下上面这个 模板的主要函数吧。  他的作用是返回编译好的模板文件路径。也就是把模板 X.html(模板文件) 用正则替换成 x.php(编译后的PHP文件).然后使用 include  函数。懂了吧! php的模板引擎都一个鸟样。 然后剩下的就是正则的东西了。等下再说。
*/
function template($module = 'phpcms', $template = 'index')
{
global $CONFIG;
$compiledtplfile = $CONFIG['templatescachedir'].$module.'_'.$template.'.tpl.php';
/**
因为phpcms是分模块来存放模板文件。所以 template 函数有两个参数: 第一个就是模块目录名,第二个就是此模块里面的模板文件名.
$CONFIG['templatescachedir']  这个是放编译后php文件存放的目录。在config.inc.php 站点配置文件里面定义的自己去看。 这样就取得了模板编译后的php文件路径。
*/
if($CONFIG['templaterefresh']) //$CONFIG['templaterefresh']  在 config.inc.php里面配置了。默认是1  。是更新模板开关。如果你设置为0 那么模板更新了。程序也不会更新。
{
        $tplfile = PHPCMS_ROOT.'/templates/'.$CONFIG['defaulttemplate'].'/'.$module.'/'.$template.'.html';
  /**
  和上面那句意思差不多。$CONFIG['defaulttemplate'] 是默认模板目录  。这句是获取你要的那个模块和里面的那个模板文件的路径(@@获取没编译前的模板文件)
  */
        if(!file_exists($compiledtplfile) || @filemtime($tplfile) > @filemtime($compiledtplfile))
  {
   /**
   我把文件编译成了php文件。那么模板改变了。 php文件总得也改变吧。要不你修改了模板后。站还是以前那个样子没变那有什么意思呢。
   首先判断模板编译文件是否存在。如果不存在那么后边那个条件不用判断了。 因为编译文件都不存在。程序肯定运行不了拉。(因为其实我们主要是运行编译后的那个php文件,模板文件是html的运行个P呀)
   或  后边那个 @filemtime($tplfile) > @filemtime($compiledtplfile)  很容易就明白:  函数 filetime() 判断文件最近修改的时间,返回Unix 时间戳。 如果模板文件的修改时间 大于 编译文件。 那么证明 模板文件  在 编译文件生成后 还进行了修改。那么我们是不是还要在更新次编译文件呀 ,那是肯定的拉。 所以继续执行下去。
   */
   require_once PHPCMS_ROOT.'/include/template.func.php';   // 加载编译函数
   template_refresh($tplfile, $compiledtplfile);// 这个就是模板的 编译启动函数 ,带动一系列的模板编译函数来最终生成模板编译文件。
  }
}
return $compiledtplfile; // 返回 模板编译后的PHP文件路径。
}

defined('IN_PHPCMS') or exit('Access Denied');
function template_compile($module,$template) //和下面那个一样是编译模板启动函数。不过两函数的参数不一样,按照上下文意思。这个函数是为了配合批量编译模板而写的。第一个是模块目录名,第二是模板文件名,解释同下。请看下面那个
{
global $CONFIG;
$content = file_get_contents(PHPCMS_ROOT.'/templates/'.$CONFIG['defaulttemplate'].'/'.$module.'/'.$template.'.html');
$content = template_parse($content);
$compiledtplfile = $CONFIG['templatescachedir'].$module.'_'.$template.'.tpl.php';
$strlen = file_put_contents($compiledtplfile, $content);
@chmod($compiledtplfile, 0777);
return $strlen;
}
function template_refresh($tplfile,$compiledtplfile) //模板编译启动函数。 参数 第一个是 模板文件名  第二个是 编译后的php文件名
{
$str = file_get_contents($tplfile); //使用了php5 的最爽函数:file_get_contents() 获取文件的内容 。
$str = template_parse($str); /*然后 使用 template_parse() 函数来对文件内容进行替换。比如把一些我们自己定义的语句:{if xx > xx}  正则替换成 <?php if(xx > xx){?>具体看下面*/
$strlen = file_put_contents($compiledtplfile, $str);//编译完成后。把内容写到我们的 那个所谓的编译PHP文件。
@chmod($compiledtplfile, 0777);  //别忘了设置下权限。
return $strlen; //返回 写到编译文件里的内容字大小节数,下面我们看下 template_parse() 函数
}
function template_module($module)//这个很有用。批量编译某模块目录下的模板文件
{
global $CONFIG;
$files = glob(PHPCMS_ROOT.'/templates/'.$CONFIG['defaulttemplate'].'/'.$module.'/*.html');
/*
glob 函数  取得 在此路径下的所有 *.html 以html为扩展名的文件列表。 具体看手册。
**/
if(is_array($files))
{
  foreach($files as $tpl)
  { //开始批量
   $template = str_replace('.html', '', basename($tpl));
   // 获取模板文件名。以次来做编译后的PHP文件名
   template_compile($module, $template); //这个函数上面讲过了。看上面
  }
}
return TRUE;
}
function template_ca