phpcms 2008 sp4的模板原理,tag的解析原理

来源:互联网 发布:匈牙利算法 指派问题 编辑:程序博客网 时间:2024/06/06 19:30

phpcms中模板解析主要是通过global.func.php,的

function template($module = 'phpcms', $template = 'index', $istag = 0){$compiledtplfile = TPL_CACHEPATH.$module.'_'.$template.'.tpl.php';  //  echo "$compiledtplfile";if(TPL_REFRESH && (!file_exists($compiledtplfile)            || @filemtime(TPL_ROOT.TPL_NAME.'/'.$module.'/'.$template.'.html') > @filemtime($compiledtplfile) ||                    @filemtime(TPL_ROOT.TPL_NAME.'/tag.inc.php') > @filemtime($compiledtplfile))){require_once PHPCMS_ROOT.'include/template.func.php';template_compile($module, $template, $istag);}return $compiledtplfile;}

函数来实现的,这其中比较重要的是template_compile函数来实现的对模板的编译,编译过过程中使用了preg_replace这个函数并加了个/e参数,对tag,get这样的标签进行了替换

$str = preg_replace("/\{tag_([^}]+)\}/e", "get_tag('\\1')", $str);$str = preg_replace("/\{get\s+([^}]+)\}/e", "get_parse('\\1')", $str);

举个例子,对于<div id="slide">{tag_首页幻灯片}</div>这个标签的解析的结果为, 

<!--幻灯片--><div id="slide"><?php echo tag('phpcms', 'tag_content_slide', "SELECT a.contentid,a.catid,a.typeid,a.areaid,a.title,a.style,a.thumb,a.keywords,a.description,a.userid,a.updatetime,a.inputtime,a.url FROM `pc_content` a, `pc_content_position` p WHERE a.contentid=p.contentid AND p.posid=2 AND a.status=99   AND  `thumb`!=''  ORDER BY a.contentid DESC", 0, 5, array (  'class' => 'url',  'target' => '_blank',  'width' => '296',  'height' => '164',));?></div><!--热点文章-->

function get_tag($tagname){global $TAG;    if(!isset($TAG)) $TAG = cache_read('tag.inc.php', TPL_ROOT.TPL_NAME.'/');return isset($TAG[$tagname]) ? '<?php echo '.$TAG[$tagname].';?>' : '{tag_'.$tagname.'}';}

模板解析后,使用了inlude并解析后的文件包含进来,解析的内容默认是从template/default/tag.inc.php中取出的内容

0 0