Discuz's template engine

Source: Internet
Author: User

discuz template engine a better template engine class, a long time ago on the Internet to find, visual inspection of the Discuz template engine should be very old, is DZ7.2 the previous version, oneself also used very handy, share this template class.

there are two files. A template class, a function that needs to be used in a template replacement
Original address: Http://blog.qita.in

  1. ? /**
  2. * Template class-parsing using Discuz template engine
  3. * http://blog.qita.in
  4. */
  5. Require_once (Dir_root. '/.. /function/template.func.php ');
  6. Class Template {
  7. Const DIR_SEP = directory_separator;
  8. /**
  9. * Template Instance
  10. *
  11. * @staticvar
  12. * @var Object Template
  13. */
  14. protected static $_instance;
  15. /**
  16. * Template parameter information
  17. *
  18. * @var Array
  19. */
  20. Protected $_options = Array ();
  21. /**
  22. * Single-piece mode invocation method
  23. *
  24. * @static
  25. * @return Object Template
  26. */
  27. public static function getinstance () {
  28. if (!self:: $_instance instanceof Self)
  29. Self:: $_instance = new self ();
  30. Return self:: $_instance;
  31. }
  32. /**
  33. * Construction Method
  34. *
  35. * @return void
  36. */
  37. Private Function __construct () {
  38. $this-_options = Array (' template_dir ' = ' templates '. Self:: DIR_SEP,//directory where template files are located
  39. ' Cache_dir ' = ' templates '. Self:: dir_sep. ' Cache '. Self:: DIR_SEP,//cache file storage Directory
  40. ' Auto_update ' = false,//if the cache is regenerated when the template file changes
  41. ' Cache_lifetime ' = 0,//Cache life cycle (minutes), 0 for permanent
  42. );
  43. }
  44. /**
  45. * Set template parameter information
  46. *
  47. * @param array $options parameter
  48. * @return void
  49. */
  50. Public function setoptions (array $options) {
  51. foreach ($options as $name = $value)
  52. $this, set ($name, $value);
  53. }
  54. /**
  55. * Set Template parameters
  56. *
  57. * @param string $name parameter name
  58. * @param mixed $value parameter values
  59. * @return void
  60. */
  61. Public function set ($name, $value) {
  62. Switch ($name) {
  63. Case ' Template_dir ':
  64. $value = $this-_trimpath ($value);
  65. if (!file_exists ($value))
  66. _throwexception, $this ("The specified template directory not found \" $value \ "");
  67. $this-_options[' template_dir ') = $value;
  68. Break
  69. Case ' Cache_dir ':
  70. $value = $this-_trimpath ($value);
  71. if (!file_exists ($value))
  72. _throwexception, $this ("The specified cache directory not found \" $value \ "");
  73. $this-_options[' cache_dir ') = $value;
  74. Break
  75. Case ' auto_update ':
  76. $this-_options[' auto_update ' = (Boolean) $value;
  77. Break
  78. Case ' Cache_lifetime ':
  79. $this, _options[' cache_lifetime ') = (float) $value;
  80. Break
  81. Default
  82. $this-_throwexception ("Unknown template configuration options \" $name \ "");
  83. }
  84. }
  85. /**
  86. * Set template Parameters by magic method
  87. *
  88. * @see Template::set ()
  89. * @param string $name parameter name
  90. * @param mixed $value parameter values
  91. * @return void
  92. */
  93. Public Function __set ($name, $value) {
  94. $this, set ($name, $value);
  95. }
  96. /**
  97. * Get template files
  98. *
  99. * @param string $file template file name
  100. * @return String
  101. */
  102. Public Function GetFile ($file) {
  103. $cachefile = $this-_getcachefile ($file);
  104. if (!file_exists ($cachefile))
  105. $this, Cache ($file);
  106. return $cachefile;
  107. }
  108. /**
  109. * Detects if the template file needs to update the cache
  110. *
  111. * @param string $file template file name
  112. * @param string $MD 5data template file MD5 checksum information
  113. * @param integer $md 5data template file expiry time check information
  114. * @return void
  115. */
  116. Public function Check ($file, $md 5data, $expireTime) {
  117. if ($this _options[' auto_update ') && md5_file ($this-_gettplfile ($file))! = $MD 5data)
  118. $this, Cache ($file);
  119. if ($this _options[' cache_lifetime ']! = 0 && (Time ()-$expireTime >= $this-_options[' Cache_lifetim E '] * 60))
  120. $this, Cache ($file);
  121. }
  122. /**
  123. * Caching of template files
  124. *
  125. * @param string $file template file name
  126. * @return void
  127. */
  128. Public Function Cache ($file) {
  129. $tplfile = $this-_gettplfile ($file);
  130. if (!is_readable ($tplfile)) {
  131. $this-_throwexception ("template file \" $tplfile \ "Not found or cannot be opened");
  132. }
  133. Get template Content
  134. $template = file_get_contents ($tplfile);
  135. Filter
  136. $template = Preg_replace ("/\<\!\-\-\{(. +?) \}\-\-\>/s "," {\\1} ", $template);
  137. Replace Language pack variables
  138. $template = Preg_replace ("/\{lang\s+ (. +?) \}/ies "," Languagevar (' \\1 ') ", $template);
  139. Replace PHP line break
  140. $template = Str_replace ("{LF}", " ", $template);
  141. Replace direct variable output
  142. $varRegexp = "((\\\$[a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*)"
  143. . "(\[[a-za-z0-9_\-\.\" \ ' \[\]\$\x7f-\xff]+\]) ";
  144. $template = Preg_replace ("/\{(\\\$[a-za-z0-9_\[\]\ ' \$\.\x7f-\xff]+) \}/s", " ", $template);
  145. $template = Preg_replace ("/$varRegexp/es", "Addquote (' )", $template);
  146. $template = Preg_replace ("/\<\?\=\<\?\= $varRegexp \?\>\?\>/es", "Addquote (' )", $template);
  147. Replace template load Command
  148. $template = Preg_replace ("/[\n\r\t]*\{template\s+ ([a-z0-9_]+) \}[\n\r\t]*/is",
  149. "\ r \ n getfile (' \\1 ')"); >\r\n ",
  150. $template
  151. );
  152. $template = Preg_replace ("/[\n\r\t]*\{template\s+ (. +?) \}[\n\r\t]*/is ",
  153. "\ r \ n getfile (\\1)); >\r\n",
  154. $template
  155. );
  156. Replace a specific function
  157. $template = Preg_replace ("/[\n\r\t]*\{eval\s+ (. +?) \}[\n\r\t]*/ies ",
  158. "Stripvtags (' , ')",
  159. $template
  160. );
  161. $template = Preg_replace ("/[\n\r\t]*\{echo\s+ (. +?) \}[\n\r\t]*/ies ",
  162. "Stripvtags (' , ')",
  163. $template
  164. );
  165. $template = Preg_replace ("/([\n\r\t]*) \{elseif\s+ (. +?) \} ([\n\r\t]*)/ies ",
  166. "Stripvtags (' \\1 \\3 ', ')",
  167. $template
  168. );
  169. $template = Preg_replace ("/([\n\r\t]*) \{else\} ([\n\r\t]*)/is",
  170. "\\1 \\2",
  171. $template
  172. );
  173. Replace loop function and conditional judgment statement
  174. $nest = 5;
  175. for ($i = 0; $i < $nest; $i + +) {
  176. $template = Preg_replace ("/[\n\r\t]*\{loop\s+ (\s+) \s+ (\s+) \}[\n\r]* (. +?) [\n\r]*\{\/loop\}[\n\r\t]*/ies],
  177. "Stripvtags (' , ' \\3 ' )",
  178. $template
  179. );
  180. $template = Preg_replace ("/[\n\r\t]*\{loop\s+ (\s+) \s+ (\s+) \s+ (\s+) \}[\n\r\t]* (. +?) [\n\r\t]*\{\/loop\}[\n\r\t]*/ies],
  181. "Stripvtags (' \\3) {?> ', ' \\4 ')",
  182. $template
  183. );
  184. $template = Preg_replace ("/([\n\r\t]*) \{if\s+ (. +?) \} ([\n\r]*) (. +?) ([\n\r]*) \{\/if\} ([\n\r\t]*)/ies ",
  185. "Stripvtags (' \\1 \\3 ', ' \\4\\5 \\6 ')",
  186. $template
  187. );
  188. }
  189. Constant substitution
  190. $template = Preg_replace ("/\{([a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*) \}/s",
  191. " ",
  192. $template
  193. );
  194. Remove extra spaces and line breaks in PHP code breaks
  195. $template = Preg_replace ("/\?\>[\n\r]*\<\? /S "," ", $template);
  196. Other replacements
  197. $template = Preg_replace ("/\" (http)? [ \w\.\/:]+\? [^\ "]+?&[^\"]+?\ "/E",
  198. "Transamp (' \\0 ')",
  199. $template
  200. );
  201. $template = Preg_replace ("/\";
  202. }
  203. function Stripblock ($var, $s) {
  204. $s = str_replace (' \ \ "', '" ', $s);
  205. $s = Preg_replace ("/<\?=\\\$ (. +?) \?>/"," {\$\\1} ", $s);
  206. Preg_match_all ("/<\?= (. +?) \?>/e ", $s, $constary);
  207. $constadd = ";
  208. $constary [1] = Array_unique ($constary [1]);
  209. foreach ($constary [1] as $const) {
  210. $constadd. = ' $__ '. $const. ' = '. $const. ';';
  211. }
  212. $s = Preg_replace ("/<\?= (. +?) \?>/"," {\$__\\1} ", $s);
  213. $s = str_replace ('?> ', "\n\$ $var. = <<
  214. $s = str_replace (' return ' );
  215. }
  216. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.