在UCH中,模板与动态数据分离,所以在很多php文件的最后,我们会看到包含了模板文件,如cp_blog.php最后有include_once template("cp_blog");
在下面的代码中,命名有规律。$tpl是没有后缀名的,$tplfile是后缀为htm的模板文件,$objfile是后缀为php的缓存文件
UCH里使用模板的流程是:
在php代码中获取动态数据,然后include_once template($tpl)
template函数解析模板文件$tplfile,返回缓存文件$objfile。
template函数中调用parse_template函数解析$tplfile,
$tplfile里有UCH定义的一套语法,在parse_template函数里可以看到,有 等。这些语法在parse_template中都会被替换成对应的函数,如readtemplate blocktags等,这些函数也都位于function_template.php中。
这里有点点纠结的就是,模板文件中可能还通过包含了其他的模板文件,所以在parse_template中要两次调用preg_replace,第一次读模板文件$tplfile所包含的子模板文件name,第二次是读子模板文件name中再包含的孙子模板文件。UCH中至多有3层模板包含关系,即父亲->儿子->孙子,所以不需要第三次调用preg_replace来读取孙子模板文件可能包含的重孙模板文件了。
template函数在function_common.php中定义
function template($name) {
global $_SCONFIG, $_SGLOBAL;
if(strexists($name,/)) {
$tpl = $name;//$name是完整目录的情况
} else {
$tpl = "template/$_SCONFIG[template]/$name";
/*
$name只是一个文件名的情况,$tpl类似template/default/cp_blog或者template/blue/cp_blog
默认的模板风格是default,但是如果用户选择了其他风格,$_SCONFIG[template]就会变成blue之类的其他值
在首页的右下角可以选择模板风格
*/
}
$objfile = S_ROOT../data/tpl_cache/.str_replace(/,_,$tpl)..php;
/*
缓存文件名,$objfile类似data/tpl_cache/template_default_cp_blog.php或者data/tpl_cache/template_blue_cp_blog.php
*/
if(!file_exists($objfile)) {
include_once(S_ROOT../source/function_template.php);
parse_template($tpl);
//如果缓存文件不存在,则对模板文件进行解析
}
return $objfile;
}
parse_template函数在function_template.php中定义
function parse_template($tpl) {
global $_SGLOBAL, $_SC, $_SCONFIG;
//包含模板
$_SGLOBAL[sub_tpls] = array($tpl);
$tplfile = S_ROOT../.$tpl..htm;
/*
$tplfile类似template/default/cp_blog.htm或者template/blue/cp_blog.htm
*/
$objfile = S_ROOT../data/tpl_cache/.str_replace(/,_,$tpl)..php;
/*
$objfile类似data/tpl_cache/template_default_cp_blog.php或者data/tpl_cache/template_blue_cp_blog.php
*/
//read
if(!file_exists($tplfile)) {
$tplfile = str_replace(/.$_SCONFIG[template]./, /default/, $tplfile);
//如果非默认模板风格的某个模板文件不存在,那么就改用default风格的该模板文件
}
$template = sreadfile($tplfile);
//读入模板文件内容
if(empty($template)) {
exit("Template file : $tplfile Not found or have no access!");
}
//模板
$template = preg_replace("//ie", "readtemplate(\1)", $template);
/*
这就是定义UCH的模板语法了,模板页中的
被替换成readtemplate(name),readtemplate函数也在function_template.php中定义。name就是([a-zA-Z0-9_/]+)
为什么多了A-Z呢,因为"//ie"最后的i选项表示不区分大小写的正则匹配
python里的正则表达式分组,似乎就是用1来表示第一组,这里用了\1
\1为什么又要用单引号裹起来呢,这是因为readtemplate函数的参数要是一个字符串
*/
//处理子页面中的代码
$template = preg_replace("//ie", "readtemplate(\1)", $template);
//解析模块调用
$template = preg_replace("//ie", "blocktags(\1)", $template);
/*
被替换成blocktags(name)
name就是(.+ ) .匹配除换行符外的任意字符,+表示出现一次或多次
表示懒惰匹配,不然后面的} - >都会被.+匹配掉
*/
//解析广告
$template = preg_replace("//ie", "adtags(\1)", $template);
/*
被替换成
如space_doing.htm里有一个被替换成了
*/
//时间处理
$template = preg_replace("//ie", "datetags(\1)", $template);
/*
被替换成datetags(name)
如space_doing.htm里有一个
被替换成了
*/
//头像处理
$template = preg_replace("//ie", "avatartags(\1)", $template);
/*
被替换成avatartags(name)
如space_doing.htm里有一个被替换成了
*/
//PHP代码
$template = preg_replace("//ies", "eva ltags(\1)", $template);
/*
被替换成eva ltags(php_expression)
php_expression就是(.+ ) 而且这里的.匹配包括换行符在内的一切字符 这是由/ies中的