首页 > 程序开发 > 【UCHome二次开发】模板解析

【UCHome二次开发】模板解析

2009年9月27日 发表评论 阅读评论

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("Template file : $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');?><?php subtplcheck('".implode('|', $_SGLOBAL['sub_tpls'])."', '$_SGLOBAL[timestamp]', '$tpl');?>$template<?php ob_out();?>";

    //write
    if(!swritefile($objfile, $template)) {
        exit("File: $objfile can not be write!");
    }
}

枫芸志原创文章,转载请注明来源并保留原文链接

本文链接:http://witmax.cn/uchome-template-analysis.html


分类: 程序开发 标签: , 9,096次阅读
  1. 2009年9月28日22:25 | #1

    纯技术的,一般看不懂,所以占完沙发就跑。。

    [回复]

  2. 2009年9月29日08:29 | #2

    @蛋王
    呵呵 最近一直在整UCHome,会连着写几篇总结

    [回复]

    小张 回复:

    怎么在uchome中调用别的库中的表吗?

    [回复]

    晴枫 回复:

    @小张, 需要新建个数据连接,可调用uchome中的mysql类,位于./source/class_mysql.php

    [回复]

    小张 回复:

    谢谢

    [回复]

  3. 我是新手
    2009年10月14日17:35 | #3

    用啥编辑器?怎么函数不变色

    [回复]

    晴枫 回复:

    @我是新手, 你说的是什么编辑器

    [回复]

  4. 李广志
    2009年10月29日22:41 | #4

    能不能对用户访问到其他用户页面是进行操作做一点解释呢,比如说点击发送消息的效果

    [回复]

    晴枫 回复:

    @李广志,如果你懂PHP的话可以看一下代码就知道怎么实现了,代码的调用机制可以参考我的两篇《【UCHome二次开发】模板修改》和《【UCHome二次开发】功能修改》;另外发送消息是调用UCenter接口,可以参考《【UCHome二次开发】与UCenter的交互解析》

    [回复]

  5. 紫贝壳
    2009年12月11日11:40 | #5

    谢谢!我市一个新手,正在做uchome二次开发,多谢您的文章。

    [回复]

  6. 小张
    2010年1月11日17:29 | #6

    uchome中我用表单提交的数据!我想让数据在页面分页显示怎么写?我的qq578379640

    [回复]

    晴枫 回复:

    @小张, 建议先读完我的【UCHome二次开发】系列文章,这样读代码应该不成问题,你要的功能UCHome有类似的,模仿一下

    [回复]

  7. walklion
    2010年2月20日22:45 | #7

    好样的,非常感谢了,最近正遇到这方面的问题找高手支持呢。谢谢了。学习

    [回复]

  8. 123
    2010年4月27日21:51 | #8

    好文章!!!!!!,谢谢了

    [回复]

  9. 2010年9月12日19:28 | #9

    好文章,本人淘网目前正在改版,谢谢了

    [回复]

  10. 2011年5月10日22:23 | #10

    公司项目需要,学习下,谢谢博主提供这么好的文章,谢谢啦。

    [回复]

  11. 清秋
    2011年8月5日16:18 | #11

    写得不错,新手路过

    [回复]

  12. 2011年8月31日08:43 | #12

    还没用过这个系统呢!看来得去研究一下。

    [回复]

  1. 2010年12月12日22:01 | #1
  2. 2012年3月9日00:03 | #2
订阅评论
  欢迎参与讨论,请在这里发表您的看法、交流您的观点。

无觅相关文章插件,快速提升流量