Discuz教程网

[二次开发] Discuz X模板解析注释

[复制链接]
authicon dly 发表于 2012-11-8 20:17:26 | 显示全部楼层 |阅读模式
discuz模板解析注释
  1. <?php

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  40. for($i = 0; $i < $nest; $i++) {
  41. $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);
  42. $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);
  43. $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);
  44. }

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

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

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

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

  54. flock($fp, 2);
  55. fwrite($fp, $template);
  56. fclose($fp);
  57. }

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

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

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

  80. ?>
复制代码



上一篇:Discuz X2 uc通信不成功,cache文件的问题
下一篇:Discuz7.2在firefox3.6不显示输入框的解决方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 07:28

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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