<?php

function parse_template($tplfile, $objfile) {
global $options;

//循环嵌套次数
$nest = 3;

//打开模板文件
if(!$fp = fopen($tplfile, ‘rb’)) {
exit(’Current template file not found or have no access!’);
}

$template = fread($fp, filesize($tplfile));
fclose($fp);

//匹配变量
//双引号(单引号)内的\具有转义所以,要得到一个\必须写为\\;要得到一个$必须写为\$;最后结果为\$,可在正则中使用的变量符号
$var_regexp = “((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\”\’\[\]\$\x7f-\xff]+\])*)”;

//匹配字符
$const_regexp = “([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)”;

//清除缩进(tab)
$template = preg_replace(”/([\n\r]+)\t+/s”, “\\1“, $template);

//清除注释(<!– –>),方便后续操作,不需要匹配多余的<!–
$template = preg_replace(”/\<\!\-\-\{(.+?)\}\-\-\>/s”, “{\\1}”, $template);

//将{LF}替换成一个硬回车(\n)
$template = str_replace(”{LF}”, “<?=\”\\n\“?>”, $template);

//匹配多种变量形式,包括$xxx[”xxxx”]与$xxx[$xxx]、或$xxx
$template = preg_replace(”/\{(\\\$[a-zA-Z0-9_\[\]\’\”\$\.\x7f-\xff]+)\}/s”, “<?=\\1?>”, $template);

//使用/e修正符,可使替换元素以php代码执行后,再进行replace.
$template = preg_replace(”/$var_regexp/es”, “addquote(’<?=\\1?>’)”, $template);

//再次替换叠加字符串变量
$template = preg_replace(”/\<\?\=\<\?\=$var_regexp\?\>\?\>/es”, “addquote(’<?=\\1?>’)”, $template);

//子模板嵌套解析
$template = preg_replace(”/[\n\r\t]*\{template\s+([a-z0-9_]+)\}[\n\r\t]*/is”, “\n<? include template(’\\1′); ?>\n”, $template);
$template = preg_replace(”/[\n\r\t]*\{template\s+(.+?)\}[\n\r\t]*/is”, “\n<? include template(\\1); ?>\n”, $template);

//eval语法解析
$template = preg_replace(”/[\n\r\t]*\{eval\s+(.+?)\}[\n\r\t]*/ies”, “stripvtags(’\n<? \\1; ?>\n’,”)”, $template);

//echo语法解析
$template = preg_replace(”/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies”, “stripvtags(’\n<? echo \\1; ?>\n’,”)”, $template);

//elseif语法解析
$template = preg_replace(”/[\n\r\t]*\{elseif\s+(.+?)\}[\n\r\t]*/ies”, “stripvtags(’\n<? } elseif(\\1) { ?>\n’,”)”, $template);

//else语法解析
$template = preg_replace(”/[\n\r\t]*\{else\}[\n\r\t]*/is”, “\n<? } else { ?>\n”, $template);

for($i = 0; $i < $nest; $i++) {
$template = preg_replace(”/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r]*(.+?)[\n\r]*\{\/loop\}[\n\r\t]*/ies”, “stripvtags(’\n<? if(is_array(\\1)) { foreach(\\1 as \\2) { ?>’,'\n\\3\n<? } } ?>\n’)”, $template);
$template = preg_replace(”/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/ies”, “stripvtags(’\n<? if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>’,'\n\\4\n<? } } ?>\n’)”, $template);
$template = preg_replace(”/[\n\r\t]*\{if\s+(.+?)\}[\n\r]*(.+?)[\n\r]*\{\/if\}[\n\r\t]*/ies”, “stripvtags(’\n<? if(\\1) { ?>’,'\n\\2\n<? } ?>\n’)”, $template);
}

//常量直接输出..
$template = preg_replace(”/\{$const_regexp\}/s”, “<?=\\1?>”, $template);

//相临定界符清除(使语法更加连贯)
$template = preg_replace(”/ \?\>[\n\r]*\<\? /s”, ” “, $template);

if(!@$fp = fopen($objfile, ‘wb’)) {
exit(’Directory \’./cache/template/\’ not found or have no access!’);
}

//转换链接中的&符号为&使编译模板读取时能够正常不会将其视为引用..
$template = preg_replace(”/\”(http)?[\w\.\/:]+\?[^\”]+?&[^\”]+?\”/e”, “transamp(’\\0′)”, $template);

flock($fp, 2);
fwrite($fp, $template);
fclose($fp);
}

//转换&避免&以引用方式执行..
function transamp($str) {
$str = str_replace(’&’, ‘&’, $str);
$str = str_replace(’&’, ‘&’, $str);
$str = str_replace(’\”‘, ‘”‘, $str);
return $str;
}

//将$var字符串,转换为可执行的php代码形式,并返回其结果..
//\”转换为”,将为[xxx]转换为[’xxx’]
function addquote($var) {
return str_replace(”\\\”", “\”", preg_replace(”/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s”, “[’\\1′]”, $var));
}

//设置语言变量
function languagevar($var) {
return $GLOBALS[’language’][$var] ? $GLOBALS[’language’][$var] : “!$var!”;
}
//清理或转换标签为php语法
function stripvtags($expr, $statement) {
$expr = str_replace(”\\\”", “\”", preg_replace(”/\<\?\=(\\\$.+?)\?\>/s”, “\\1“, $expr));
$statement = str_replace(”\\\”", “\”", $statement);
return $expr.$statement;
}

?>

转载于:https://www.cnblogs.com/vicenteforever/articles/1614489.html

discuz模板解析注释相关推荐

  1. Discuz模板引擎标签

    界面风格与模板定制的详细说明 Discuz! 的模板采用文件保存,默认模板保存在 ./templates/default/ 目录中,该目录下 *.htm 文件是模板文件,*.lang.php 是语言包 ...

  2. php 模板解析,关于模板的原理和解析

    PHP – 关于模板的原理和解析 将PHP代码和静态HTML代码进行分离,使代码的可读性和维护性得到显著提高. 使用模板引擎: 我们所说的模板是web模板,是主要由HTML标记组成的语言来编写的语言, ...

  3. discuz模板机制应用

    discuz用多了,觉得它的模板确实好用,便将它的数据库操作.模板提取出来,小组了一下,用于单独的开发用. 经过一个商城网站的开发,应用已经非常顺手了.商城主站完成后,进行代理网站的开发,其实主要就是 ...

  4. PHP模板解析对象,PHP模板解析类实例

    摘要:本文实例讲述了PHP模板解析类.分享给大家供大家参考.具体如下:<?php class template { private $vars = array(); private $conf  ...

  5. sdcms的模板解析引擎,一个非常简单和实用的CMS

    代码 <% '============================== 'SDCMS模板解析引擎 'Author:IT平民 'Date:2009年4-5月 '================ ...

  6. vmwear导出OVF模板解析(解决ovf导入服务器失败问题,虚拟机版本等)

    vmwear导出OVF模板解析(解决ovf导入服务器失败问题,虚拟机版本等) 参考文章: (1)vmwear导出OVF模板解析(解决ovf导入服务器失败问题,虚拟机版本等) (2)https://ww ...

  7. B75经典门户商业版discuz模板

    简介: B75经典门户商业版discuz模板,支持discuzx3.0版本,discuzx3.1版本,discuzx3.2版本,包括[门户首页].[门户文章列表].[门户文章详情].[论坛首页].[论 ...

  8. php 反射类 解析注释,php反射获取类和方法中的注释

    通过php中的反射机制,获取该类的文档注释,再通过获取其所有的方法,获取方法的注释 所用到的主要类及其方法 ReflectionClass ReflectionClass::getDocComment ...

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

    UCHome模板文件位于/template文件夹下,每个模板文件单独一个文件夹,默认模板文件夹为default. 1.模板的使用配置 在根目录下的config.php中进行配 $_SC['templa ...

最新文章

  1. 如何看待计算机视觉未来的走向?
  2. 如何学好 Linux、C++,并搞定 BAT 面试 作者/分享人:天千
  3. 关于在线预览word,excel,ppt,pdf的需求处理方法。
  4. php通过QQ号获取QQ信息,通过openId能获取到QQ号码吗?
  5. git恢复删除文件之ls-files
  6. gcc/g++静态链接和动态链接解决glibc版本不兼容的问题
  7. YOLOX——Win10下训练自定义VOC数据集
  8. sonarqube启动报错解决办法:Process exited with exit value [es]: 143
  9. sqlite3_setp
  10. 数据科学 IPython 笔记本 9.9 花式索引
  11. 爬虫python入门_python爬虫入门教程有哪些?适合的只有这三个
  12. 蓝桥杯 2018年预赛C语言大学B组 C/C++
  13. easyui首页模板
  14. maven 命令下载jar包(mvn命令根据依赖下载jar包)
  15. [BZOJ4816][Sdoi2017]数字表格 数学
  16. application octet stream java_Java servlet句柄application/octet-stream用于图像上传
  17. Win11如何关闭自动更新?Win11更新服务关闭教程
  18. JavaScript 专题(九)数组中查找指定元素
  19. tophat以及cufflinks的使用
  20. java基本类型char

热门文章

  1. 断点续传了解一下啊?
  2. git 服务器自动部署项目之GitHooks
  3. [CSAcademy]A-Game
  4. Visual Studio2005的加载此属性页时出错的解决办法(转)
  5. tomcat WARNING [ContainerBackgroundProcessor[StandardEngine[Catalina]]]
  6. Spring MVC POJO传参方式
  7. innodb表空间结构
  8. 每日英语:China's Youth to Employers: I Quit
  9. OracleDBConsoleorcl服务无法启动问题(1053错误、发生服务特定错误2)
  10. Excel对重复数据分组,求出不同的数据(office 2013)