您的位置:首页 > 其它

第五章:【UCHome二次开发】模板解析

2011-09-06 15:43 246 查看
UCHome模板文件位于/template文件夹下,每个模板文件单独一个文件夹,默认模板文件夹为default。

1、模板的使用配置

在根目录下的config.php中进行配

$_SC['template'] = 'default'; //选择模板目录

2、模板的处理

程序中使用到模板文件时,先去模板缓存目录/data/tpl_cache/下查找是否存储模板缓存文件。模板缓存文件命名合适为“template_模板目录名_模板文件名.php”。如存在则直接使用该缓存的模板文件;如不存在,则先解析对应的模板文件,生成模板缓存文件再进行使用。
3、模板的解析
模板解析是调用/source目录下的function_template.php文件中的parse_template函数来实现的。
解析过程并不复杂,主要是读取模板文件(.htm),用正则表达式替换标记为对应的PHP代码,最终生成一个标准的PHP文件,保存到模板缓存目录/data/tpl_cache/供后续使用。
具体的模板解析过程不做说明,直接查看代码即可。
置,确定系统使用的模板,如下:
function parse_template($tpl) {

global $_SGLOBAL;

//包含模板

$_SGLOBAL['sub_tpls'] =array($tpl);

$tplfile = S_ROOT.'./'.$tpl.'.htm';

$objfile =S_ROOT.'./data/tpl_cache/'.str_replace('/','_',$tpl).'.php';

//read

$template =sreadfile($tplfile);

if(empty($template)) {

exit("Templatefile : $tplfile Not found or have no access!");

}

//模板

$template =preg_replace("/\<\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\>/ie","readtemplate('\\1')", $template);

//处理子页面中的代码

$template =preg_replace("/\<\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\>/ie","readtemplate('\\1')", $template);

//解析模块调用

$template =preg_replace("/\<\!\-\-\{block\/(.+?)\}\-\-\>/ie","blocktags('\\1')", $template);

//解析广告

$template =preg_replace("/\<\!\-\-\{ad\/(.+?)\}\-\-\>/ie","adtags('\\1')", $template);

//时间处理

$template =preg_replace("/\<\!\-\-\{date\((.+?)\)\}\-\-\>/ie","datetags('\\1')", $template);

//头像处理

$template =preg_replace("/\<\!\-\-\{avatar\((.+?)\)\}\-\-\>/ie","avatartags('\\1')", $template);

//PHP代码

$template =preg_replace("/\<\!\-\-\{eval\s+(.+?)\s*\}\-\-\>/ies","evaltags('\\1')", $template);

//开始处理

//变量

$var_regexp ="((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";

$template =preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}",$template);

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

$template =preg_replace("/(\\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\.([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/s", "\\1['\\2']",$template);

$template =preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s","<?=\\1?>", $template);

$template =preg_replace("/$var_regexp/es","addquote('<?=\\1?>')", $template);

$template =preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es","addquote('<?=\\1?>')", $template);

//逻辑

$template =preg_replace("/\{elseif\s+(.+?)\}/ies", "stripvtags('<?php }elseif(\\1) { ?>','')", $template);

$template =preg_replace("/\{else\}/is", "<?php } else { ?>",$template);

//循环

for($i = 0; $i < 5;$i++) {

$template =preg_replace("/\{loop\s+(\S+)\s+(\S+)\}(.+?)\{\/loop\}/ies","stripvtags('<?php if(is_array(\\1)) { foreach(\\1 as \\2) {?>','\\3<?php } } ?>')", $template);

$template =preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}(.+?)\{\/loop\}/ies","stripvtags('<?php if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) {?>','\\4<?php } } ?>')", $template);

$template =preg_replace("/\{if\s+(.+?)\}(.+?)\{\/if\}/ies","stripvtags('<?php if(\\1) { ?>','\\2<?php } ?>')",$template);

}

//常量

$template =preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s","<?=\\1?>", $template);

//替换

if(!empty($_SGLOBAL['block_search'])) {

$template =str_replace($_SGLOBAL['block_search'], $_SGLOBAL['block_replace'], $template);

}

//换行

$template =preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);

//附加处理

$template ="<?php if(!defined('IN_UCHOME')) exit('Access Denied');?><?phpsubtplcheck('".implode('|', $_SGLOBAL['sub_tpls'])."', '$_SGLOBAL[timestamp]','$tpl');?>$template<?php ob_out();?>";

//write

if(!swritefile($objfile, $template)) {

exit("File:$objfile can not be write!");

}

}


所有章节下载:http://download.csdn.net/source/3579636

文章摘自:http://wenku.baidu.com/view/ffd8a5c14028915f804dc2e0.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: