找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 4599|回复: 3

class_template.php怎么使用的?dz如何实现模板调用的?

  [复制链接]
发表于 2015-6-28 12:14:17 | 显示全部楼层 |阅读模式
问题如标题所说,求大神解答
回复

使用道具 举报

发表于 2015-6-28 13:30:47 | 显示全部楼层
1、模板的使用配置
在根目录下的config.php中进行配置,确定系统使用的模板,如下:
  1. <font size="3">$_SC['template'] = 'default'; //选择模板</font>
复制代码

2、模板的处理
程序中使用到模板文件时,先去模板缓存目录/data/tpl_cache/下查找是否存储模板缓存文件。模板缓存文件命名合适为“template_模板目录名_模板文件名.php”。如存在则直接使用该缓存的模板文件;如不存在,则先解析对应的模板文件,生成模板缓存文件再进行使用。

3、模板的解析
模板解析是调用/source目录下的function_template.php文件中的parse_template函数来实现的。
解析过程并不复杂,主要是读取模板文件(.htm),用正则表达式替换标记为对应的PHP代码,最终生成一个标准的PHP文件,保存到模板缓存目录/data/tpl_cache/供后续使用。
具体的模板解析过程不做说明,直接查看代码即可。
  1. function parse_template($tpl) {
  2.     global $_SGLOBAL;

  3.     //包含模板
  4.     $_SGLOBAL['sub_tpls'] = array($tpl);

  5.     $tplfile = S_ROOT.'./'.$tpl.'.htm';
  6.     $objfile = S_ROOT.'./data/tpl_cache/'.str_replace('/','_',$tpl).'.php';
  7.    
  8.     //read
  9.     $template = sreadfile($tplfile);
  10.     if(empty($template)) {
  11.         exit("Template file : $tplfile Not found or have no access!");
  12.     }

  13.     //模板
  14.     $template = preg_replace("/\<\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\>/ie", "readtemplate('\\1')", $template);
  15.     //处理子页面中的代码
  16.     $template = preg_replace("/\<\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\>/ie", "readtemplate('\\1')", $template);
  17.     //解析模块调用
  18.     $template = preg_replace("/\<\!\-\-\{block\/(.+?)\}\-\-\>/ie", "blocktags('\\1')", $template);
  19.     //解析广告
  20.     $template = preg_replace("/\<\!\-\-\{ad\/(.+?)\}\-\-\>/ie", "adtags('\\1')", $template);
  21.     //时间处理
  22.     $template = preg_replace("/\<\!\-\-\{date\((.+?)\)\}\-\-\>/ie", "datetags('\\1')", $template);
  23.     //头像处理
  24.     $template = preg_replace("/\<\!\-\-\{avatar\((.+?)\)\}\-\-\>/ie", "avatartags('\\1')", $template);
  25.     //PHP代码
  26.     $template = preg_replace("/\<\!\-\-\{eval\s+(.+?)\s*\}\-\-\>/ies", "evaltags('\\1')", $template);

  27.     //开始处理
  28.     //变量
  29.     $var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\."\'\[\]\$\x7f-\xff]+\])*)";
  30.     $template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
  31.     $template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
  32.     $template = preg_replace("/(\\\$[a-zA-Z0-9_\[\]\'"\$\x7f-\xff]+)\.([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/s", "\\1['\\2']", $template);
  33.     $template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'"\$\.\x7f-\xff]+)\}/s", "<?=\\1?>", $template);
  34.     $template = preg_replace("/$var_regexp/es", "addquote('<?=\\1?>')", $template);
  35.     $template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "addquote('<?=\\1?>')", $template);
  36.     //逻辑
  37.     $template = preg_replace("/\{elseif\s+(.+?)\}/ies", "stripvtags('<?php } elseif(\\1) { ?>','')", $template);
  38.     $template = preg_replace("/\{else\}/is", "<?php } else { ?>", $template);
  39.     //循环
  40.     for($i = 0; $i < 5; $i++) {
  41.         $template = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}(.+?)\{\/loop\}/ies", "stripvtags('<?php if(is_array(\\1)) { foreach(\\1 as \\2) { ?>','\\3<?php } } ?>')", $template);
  42.         $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);
  43.         $template = preg_replace("/\{if\s+(.+?)\}(.+?)\{\/if\}/ies", "stripvtags('<?php if(\\1) { ?>','\\2<?php } ?>')", $template);
  44.     }
  45.     //常量
  46.     $template = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s", "<?=\\1?>", $template);
  47.    
  48.     //替换
  49.     if(!empty($_SGLOBAL['block_search'])) {
  50.         $template = str_replace($_SGLOBAL['block_search'], $_SGLOBAL['block_replace'], $template);
  51.     }
  52.    
  53.     //换行
  54.     $template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);
  55.    
  56.     //附加处理
  57.     $template = "<?php if(!defined('IN_UCHOME')) exit('Access Denied');?><?php subtplcheck('".implode('|', $_SGLOBAL['sub_tpls'])."', '$_SGLOBAL[timestamp]', '$tpl');?>$template<?php ob_out();?>";
  58.    
  59.     //write
  60.     if(!swritefile($objfile, $template)) {
  61.         exit("File: $objfile can not be write!");
  62.     }
  63. }
复制代码


回复

使用道具 举报

 楼主| 发表于 2015-6-28 18:58:22 | 显示全部楼层
技术帮助 发表于 2015-6-28 13:30
1、模板的使用配置在根目录下的config.php中进行配置,确定系统使用的模板,如下:
2、模板的处理程序中使 ...

文件没找到哎...我用的是dz3.2的代码,您确定用的这个?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|Discuz代码分析|Discuz目录结构|DZ起点网 ( 蜀ICP备13000518号-15 )

GMT+8, 2025-5-3 17:40 , Processed in 0.185165 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表