当前位置:无线网络首页 >> 软件开发 >> PHPCMS2008源码浅析
PHPCMS2008源码浅析 (9)
2009-07-21 16:39:54  作者:merlang  来源:数码人家整理  浏览次数:2207  文字大小:【】【】【

PHPCMS2008源码浅析之模版原理 template.func.php

一、config.inc.php 里面关于模版的相关配置变量

//模板相关配置

define('TPL_ROOT', PHPCMS_ROOT.'templates/'); //模板保存物理路径

define('TPL_NAME', 'default'); //当前模板方案目录

define('TPL_CSS', 'default'); //当前样式目录

define('TPL_CACHEPATH', PHPCMS_ROOT.'data/cache_template/'); //模板缓存物理路径

define('TPL_REFRESH', 1); //是否开启模板缓存自动刷新

 

二、global.func.php 的相关调用函数

 

function template($module = 'phpcms', $template = 'index', $istag = 0)    //模版调用函数(模块名,模版名,是否tag)

{

        $compiledtplfile = TPL_CACHEPATH.$module.'_'.$template.'.tpl.php';       //根据参数生成cache模板php文件

        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;                                //返回模板生成文件.php文件

}

 

function tpl_data($module = 'phpcms', $template = 'index')   //生成tpl的数据模板

{

        @extract($GLOBALS, EXTR_SKIP);

        ob_start();

        include template($module, $template);

        $data = ob_get_contents();

        ob_clean();

        return $data;

}

 

三、template.func.php 的相关函数

<?php

function template_compile($module, $template, $istag = 0)

{

        $tplfile = TPL_ROOT.TPL_NAME.'/'.$module.'/'.$template.'.html';   //生成模版名

        $content = @file_get_contents($tplfile);                          //加载模版文件

        if($content === false) showmessage("$tplfile is not exists!");

        $compiledtplfile = TPL_CACHEPATH.$module.'_'.$template.'.tpl.php';  //生成模版cache文件名

        $content = ($istag || substr($template, 0, 4) == 'tag_') ? '<?php function _tag_'.$module.'_'.$template.'($data, $number, $rows, $count, $page, $pages, $setting){ global $PHPCMS,$MODULE,$M,$CATEGORY,$TYPE,$AREA,$GROUP,$MODEL,$templateid,$_userid,$_username;@extract($setting);?>'.template_parse($content, 1).'<?php } ?>' : template_parse($content);

        //用template_parse正则替换后,形成可以执行的php文件

        $strlen = file_put_contents($compiledtplfile, $content); //写到cache里

        @chmod($compiledtplfile, 0777);

        return $strlen;                       //返回生成值

}

 

function template_refresh($tplfile, $compiledtplfile)   //单个模版文件的更新

{

        $str = file_get_contents($tplfile);

        $str = template_parse($str);

        $strlen = file_put_contents($compiledtplfile, $str);

        @chmod($compiledtplfile, 0777);

        return $strlen;

}

 

function template_module($module)                              //更新一个模块的所有缓存文件

{

        $files = glob(TPL_ROOT.TPL_NAME.'/'.$module.'/*.html');    //征收成文件名数组

        if(is_array($files))

        {

                foreach($files as $tpl)

                {

                        $template = str_replace('.html', '', basename($tpl));   //正则取文件名,去掉.html

                        template_compile($module, $template);                  //重新生成module的cache

                }

        }

        return TRUE;

}

 

function template_cache()                         //生成所能的模块cache文件

{

    global $MODULE;                               //在引入common.inc.php 中已经引入comman.php文件,所有会存在此变量,全局变量module 可能是调用的cache.func生成的文件下的变量引入的,暂时没明,下回分解

        foreach($MODULE as $module=>$m)

    {

        template_module($module);

        }

        return TRUE;

}

 

function template_block($blockid)                  

{

        $tplfile = TPL_ROOT.TPL_NAME.'/phpcms/block/'.$blockid.'.html';

        $compiledtplfile = TPL_CACHEPATH.'phpcms_block_'.$blockid.'.tpl.php';

        if(TPL_REFRESH && (!file_exists($compiledtplfile) || @filemtime($tplfile) > @filemtime($compiledtplfile)))

        {

                template_refresh($tplfile, $compiledtplfile);

        }

        return $compiledtplfile;

}

 

function template_parse($str, $istag = 0)     

//此函数的最终目地是将,模版文件.html文件,形成/daba/data_templete/module_name.tpl.php 文件,也就是一些正则的转换,和dz的模式差不多

{

        $str = preg_replace("/([ ]+)\t+/s","\\1",$str);

        $str = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}",$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);

        $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/","<?php if(is_array(\\1)) foreach(\\1 AS \\2) { ?>",$str);

        $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/","<?php if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>",$str);

        $str = preg_replace("/\{\/loop\}/","<?php } ?>",$str);

        $str = preg_replace("/\{\/get\}/","<?php } unset(\$DATA); ?>",$str);

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

        $str = preg_replace("/\{get\s+([^}]+)\}/e", "get_parse('\\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-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","<?php echo \\1;?>",$str);

        $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es", "addquote('<?php echo \\1;?>')",$str);

        $str = preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>",$str);

        if(!$istag) $str = "<?php defined('IN_PHPCMS') or exit('Access Denied'); ?>".$str;

        return $str;

}

 

function get_tag($tagname)   //获取phpcms的tag数据

{

        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.'}';

}

 

function addquote($var)          //变量数据加双相号

{

        return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));

}

 

function get_parse($str)  //正则匹配

{

        preg_match_all("/([a-z]+)\=\"([^\"]+)\"/i", stripslashes($str), $matches, PREG_SET_ORDER);

        foreach($matches as $v)

        {

                $r[$v[1]] = $v[2];

        }

        extract($r);

        if(!isset($dbsource)) $dbsource = '';

        if(!isset($dbname)) $dbname = '';

        if(!isset($sql)) $sql = '';

        if(!isset($rows)) $rows = 0;

        if(!isset($return) || !preg_match("/^\w+$/i", $return)) $return = 'r';

        if(isset($page))

        {

            $str = "<?php \$ARRAY = get(\"$sql\", $rows, $page, \"$dbname\", \"$dbsource\");\$DATA=\$ARRAY['data'];\$total=\$ARRAY['total'];\$count=\$ARRAY['count'];\$pages=\$ARRAY['pages'];unset(\$ARRAY);foreach(\$DATA AS \$n=>\${$return}){\$n++;?>";

        }

        else

        {

                $str = substr($str, -1) == '/' ? "<?php \${$return} = get(\"$sql\", -1, 0, \"$dbname\", \"$dbsource\");?>" : "<?php \$DATA = get(\"$sql\", $rows, 0, \"$dbname\", \"$dbsource\");foreach(\$DATA AS \$n => \${$return}) { \$n++;?>";

        }

        return $str;

}

?>

 

 

再看首页文件index.pbp

 

<?php

......

 

include template('phpcms', 'index');   

// 这里就把生成的php文件全部加载了进来,并且会生成我们看到的首页,但这个首页只是形成了首页cache ,

//还要用 /include/html.class.php 中将它生成静态文件,更新首页后,是不是根目录有一个index.html文件,其实就是这个函数生成的

        function index()  //更新首页,在根目录生成index.html文件

        {

                extract($GLOBALS, EXTR_SKIP);

                $head['title'] = $PHPCMS['sitename'].'-'.$PHPCMS['meta_title'];

                $head['keywords'] = $PHPCMS['meta_keywords'];

                $head['description'] = $PHPCMS['meta_description'];

                $subcats = subcat('phpcms', 0, 0);

                $catid = 0;

                ob_start();

                include template('phpcms', 'index');

                $file = PHPCMS_ROOT.$this->url->index();

                return createhtml($file);

        }

 

...?>
 

 

0

顶一下

0

踩一下
[1] [2] [3] [4] [5] [6] [7] [8] [9]
数码人家 | 版权声明 | 关于我们 | 友情链接 | 联系我们 | 广告服务 | 七年级下册

  • Copyright©2003-2018, 数码人家-网园 All Rights Reserved. 远山投姿欢迎您!
    陕ICP备05016083号-2
  •