第一步:前台首页默认执行的是:index.php?m=content&c=index&a=init,代码如下:
- //首页
- public function init() {
- if(isset($_GET['siteid'])) {
- $siteid = intval($_GET['siteid']); //当前站点ID
- } else {
- $siteid = 1; //当前站点ID
- }
- $siteid = $GLOBALS['siteid'] = max($siteid,1);
- define('SITEID', $siteid);
- $_userid = $this->_userid;
- $_username = $this->_username;
- $_groupid = $this->_groupid;
- $SEO = seo($siteid); //查看第二步,获取当前站点当前栏目下生成的SEO信息
- $sitelist = getcache('sitelist','commons'); //缓存后台设置的所有站点配置信息
- $default_style = $sitelist[$siteid]['default_style']; //当前站点默认模板风格配置
- $CATEGORYS = getcache('category_content_'.$siteid,'commons'); //当前站点所有栏目详细配置信息
- include template('content','index',$default_style); //查看第三步:模版调用
- }
第二步:获取SEO信息:phpcms/libs/functions/global.func.php,代码如下:
- /**
- * 生成SEO
- * @param $siteid 站点ID
- * @param $catid 栏目ID
- * @param $title 标题
- * @param $description 描述
- * @param $keyword 关键词
- */
- function seo($siteid, $catid = '', $title = '', $description = '', $keyword = '') {
- if (!emptyempty($title))$title = strip_tags($title); //过滤title
- if (!emptyempty($description)) $description = strip_tags($description); //过滤description
- if (!emptyempty($keyword)) $keyword = str_replace(' ', ',', strip_tags($keyword));//过滤keyword
- $sites = getcache('sitelist', 'commons'); //所有站点详细配置信息
- $site = $sites[$siteid]; //当前站点详细配置信息
- $cat = array();
- if (!emptyempty($catid)) { //栏目ID不为空
- $siteids = getcache('category_content','commons'); //所有栏目对应的站点ID缓存文件,格式:栏目ID=>站点ID
- $siteid = $siteids[$catid]; //当前栏目对应的站点ID
- $categorys = getcache('category_content_'.$siteid,'commons'); //当前站点下所有栏目的详细配置信息
- $cat = $categorys[$catid]; //当前站点下当前栏目的详细配置信息
- $cat['setting'] = string2array($cat['setting']); //当前站点当前栏目详细配置信息的setting设置信息,转化为数组
- }
- //站点title
- $seo['site_title'] =isset($site['site_title']) && !emptyempty($site['site_title']) ? $site['site_title'] : $site['name'];
- //关键词
- $seo['keyword'] = !emptyempty($keyword) ? $keyword : $site['keywords'];
- //描述
- $seo['description'] = isset($description) && !emptyempty($description) ? $description : (isset($cat['setting']['meta_description']) && !emptyempty($cat['setting']['meta_description']) ? $cat['setting']['meta_description'] : (isset($site['description']) && !emptyempty($site['description']) ? $site['description'] : ''));
- //标题
- $seo['title'] = (isset($title) && !emptyempty($title) ? $title.' - ' : '').(isset($cat['setting']['meta_title']) && !emptyempty($cat['setting']['meta_title']) ? $cat['setting']['meta_title'].' - ' : (isset($cat['catname']) && !emptyempty($cat['catname']) ? $cat['catname'].' - ' : ''));
- foreach ($seo as $k=>$v) {
- $seo[$k] = str_replace(array("/n","/r"), '', $v); //将seo信息中/n和/r替换为空
- }
- return $seo; //返回seo数组信息
- }
第三步:模板调用:phpcms/libs/functions/global.func.php,代码如下:
- /**
- * 模板调用
- *
- * @param $module
- * @param $template
- * @param $istag
- * @return unknown_type
- */
- function template($module = 'content', $template = 'index', $style = '') {
- if(strpos($module, 'plugin/')!== false) { //一般情况下不会执行if里面的代码
- $plugin = str_replace('plugin/', '', $module);
- return p_template($plugin, $template,$style);
- }
- $module = str_replace('/', DIRECTORY_SEPARATOR, $module);
- if(!emptyempty($style) && preg_match('/([a-z0-9/-_]+)/is',$style)) {//如果模板风格不为空
- } elseif (emptyempty($style) && !defined('STYLE')) { //如果模板风格为空
- if(defined('SITEID')) { //是否定义了SITEID常量
- $siteid = SITEID;
- } else {
- $siteid = param::get_cookie('siteid');
- }
- if (!$siteid) $siteid = 1;
- $sitelist = getcache('sitelist','commons'); //获取所有站点的详细配置信息
- if(!emptyempty($siteid)) {
- $style = $sitelist[$siteid]['default_style']; //获取当前站点的默认模板风格
- }
- } elseif (emptyempty($style) && defined('STYLE')) {
- $style = STYLE;
- } else {
- $style = 'default';
- }
- if(!$style) $style = 'default';
- $template_cache = pc_base::load_sys_class('template_cache');//模板解析类,路径:phpcms/libs/classes/template_cache.class.php
- //编译文件缓存路径:根目录/caches/caches_template/default/content/index.php
- $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';
- //路径:phpcms/templates/dafault/content/index.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);//查看第四步:适用模板风格不是default的情况
- }
- } else {
- //编译文件缓存路径:根目录/caches/caches_template/default/content/index.php
- $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');//查看第四步:适用于模板风格为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');
- } //Vevb.com
- }
- //返回编译文件
- return $compiledtplfile;
- }
第四步:模板解析:phpcms/libs/classes/template_cache.class.php,代码如下:
- /**
- * 编译模板
- *
- * @param $module 模块名称
- * @param $template 模板文件名
- * @param $istag 是否为标签模板
- * @return unknown
- */
- public function template_compile($module, $template, $style = 'default') {
- if(strpos($module, '/')=== false) {//如果"/"不存在
- //路径:phpcms/templates/default/content/index.html ,如:首页公共模板文件
- $tplfile = $_tpl = PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';
- } elseif (strpos($module, 'yp/') !== false) {
- $module = str_replace('/', DIRECTORY_SEPARATOR, $module);
- $tplfile = $_tpl = PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';
- } else {
- $plugin = str_replace('plugin/', '', $module);
- $module = str_replace('/', DIRECTORY_SEPARATOR, $module);
- $tplfile = $_tpl = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$template.'.html';
- }
- if ($style != 'default' && !file_exists ( $tplfile )) {
- $style = 'default';
- $tplfile = PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';
- }
- if (! file_exists ( $tplfile )) {
- //如果公共模板文件不存在,则提示模板文件不存在,如:/templates/default/content/index.html is not exists!
- showmessage ( "templates".DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.".html is not exists!" );
- }
- //获取公共模板文件中的内容
- $content = @file_get_contents ( $tplfile );
- //要生成的编译文件所在目录
- $filepath = CACHE_PATH.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR;
- if(!is_dir($filepath)) {
- //如果目录不存在,则层级创建所有目录
- mkdir($filepath, 0777, true);
- }
- //编译文件的全路径
- $compiledtplfile = $filepath.$template.'.php';
- //解析公共模板文件中的内容及标签,并返回解析后的内容
- $content = $this->template_parse($content);//查看第五步
- //将解析后的公共模板文件内容写入到要生成的编译文件中
- $strlen = file_put_contents ( $compiledtplfile, $content );
- //给生成的编译文件设置权限
- chmod ( $compiledtplfile, 0777 );
- return $strlen;//返回写入编译文件的字节数
- }
第五步:模板解析:phpcms/libs/classes/template_cache.class.php,代码如下:
- /**
- * 解析模板
- *
- * @param $str 模板内容
- * @return ture
- */
- public function template_parse($str) {
- $str = preg_replace ( "//{template/s+(.+)/}/", "<?php include template(//1); ?>", $str );
- $str = preg_replace ( "//{include/s+(.+)/}/", "<?php include //1; ?>", $str );
- $str = preg_replace ( "//{php/s+(.+)/}/", "<?php //1?>", $str );
- $str = preg_replace ( "//{if/s+(.+?)/}/", "<?php if(//1) { ?>", $str );
- $str = preg_replace ( "//{else/}/", "<?php } else { ?>", $str );
- $str = preg_replace ( "//{elseif/s+(.+?)/}/", "<?php } elseif (//1) { ?>", $str );
- $str = preg_replace ( "//{//if/}/", "<?php } ?>", $str );
- //for 循环
- $str = preg_replace("//{for/s+(.+?)/}/","<?php for(//1) { ?>",$str);
- $str = preg_replace("//{//for/}/","<?php } ?>",$str);
- //++ --
- $str = preg_replace("//{/+/+(.+?)/}/","<?php ++//1; ?>",$str);
- $str = preg_replace("//{/-/-(.+?)/}/","<?php ++//1; ?>",$str);
- $str = preg_replace("//{(.+?)/+/+/}/","<?php //1++; ?>",$str);
- $str = preg_replace("//{(.+?)/-/-/}/","<?php //1--; ?>",$str);
- $str = preg_replace ( "//{loop/s+(/S+)/s+(/S+)/}/", "<?php /$n=1;if(is_array(//1)) foreach(//1 AS //2) { ?>", $str );
- $str = preg_replace ( "//{loop/s+(/S+)/s+(/S+)/s+(/S+)/}/", "<?php /$n=1; if(is_array(//1)) foreach(//1 AS //2 => //3) { ?>", $str );
- $str = preg_replace ( "//{//loop/}/", "<?php /$n++;}unset(/$n); ?>", $str );
- $str = preg_replace ( "//{([a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7f-/xff:]*/(([^{}]*)/))/}/", "<?php echo //1;?>", $str );
- $str = preg_replace ( "//{//$([a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7f-/xff:]*/(([^{}]*)/))/}/", "<?php echo //1;?>", $str );
- $str = preg_replace ( "//{(//$[a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7f-/xff]*)/}/", "<?php echo //1;?>", $str );
- $str = preg_replace("//{(//$[a-zA-Z0-9_/[/]/'/"/$/x7f-/xff]+)/}/es", "/$this->addquote('<?php echo //1;?>')",$str);
- $str = preg_replace ( "//{([A-Z_/x7f-/xff][A-Z0-9_/x7f-/xff]*)/}/s", "<?php echo //1;?>", $str );
- $str = preg_replace("//{pc:(/w+)/s+([^}]+)/}/ie", "self::pc_tag('$1','$2', '$0')", $str);//查看第六步:解析pc标签的开始标签
- $str = preg_replace("//{//pc/}/ie", "self::end_pc_tag()", $str);//查看第六步:解析pc标签的结束标签
- $str = "<?php defined('IN_PHPCMS') or exit('No permission resources.'); ?>" . $str;
- return $str;
- }
第六步:pc标签的解析,代码如下:
新闻热点
疑难解答