Template engine of Discuz

Source: Internet
Author: User
Template engine of Discuz

The template engine of Discuz is a good template engine class. it was found on the Internet a long time ago. the template engine of Discuz should have been very old, and it was a version earlier than DZ7.2, I am also very comfortable with using this template class.

There are two files. A template class, a template replacement function
Address: http://blog.qita.in

  1. ? /**
  2. * Template class-use the Discuz template engine for parsing
  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 call 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. * Constructor
  34. *
  35. * @ Return void
  36. */
  37. Private function _ construct (){
  38. $ This-> _ options = array ('Template _ dir' => 'templates'. self: DIR_SEP, // Directory of the template file
  39. 'Cache _ dir' => 'templates'. self: DIR_SEP. 'cache'. self: DIR_SEP, // cache file storage directory
  40. 'Auto _ update' => false, // whether to regenerate the cache when the template file is changed
  41. 'Cache _ lifetime' => 0, // cache lifecycle (minutes), 0 indicates permanent
  42. );
  43. }
  44. /**
  45. * Set template parameter information
  46. *
  47. * @ Param array $ options parameter array
  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 value
  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. $ This-> _ throwException ("The specified template directory \" $ value \ "" is not found \"");
  67. $ This-> _ options ['Template _ dir'] = $ value;
  68. Break;
  69. Case 'cache _ dir ':
  70. $ Value = $ this-> _ trimpath ($ value );
  71. If (! File_exists ($ value ))
  72. $ This-> _ throwException ("The specified cache directory \" $ value \ "" is not found \"");
  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
  87. *
  88. * @ See Template: set ()
  89. * @ Param string $ name parameter name
  90. * @ Param mixed $ value parameter value
  91. * @ Return void
  92. */
  93. Public function _ set ($ name, $ value ){
  94. $ This-> set ($ name, $ value );
  95. }
  96. /**
  97. * Getting 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. * Checks whether the template file needs to update the cache.
  110. *
  111. * @ Param string $ file template file name
  112. * @ Param string $ md5 verification information of the md5data template file
  113. * @ Param integer $ md5data template file expiration time verification information
  114. * @ Return void
  115. */
  116. Public function check ($ file, $ md5data, $ expireTime ){
  117. If ($ this-> _ options ['auto _ update'] & md5_file ($ this-> _ getTplFile ($ file ))! = $ Md5data)
  118. $ This-> cache ($ file );
  119. If ($ this-> _ options ['cache _ lifetime']! = 0 & (time ()-$ expireTime >=$ this-> _ options ['cache _ lifetime'] * 60 ))
  120. $ This-> cache ($ file );
  121. }
  122. /**
  123. * Cache 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. // Obtain the 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 "," your agevar ('\ 1') ", $ template );
  139. // Replace the PHP line break
  140. $ Template = str_replace ("{LF }"," ", $ Template );
  141. // Replace the 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 the 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 the cyclic function and condition judgment statement
  174. $ Nest = 5;
  175. For ($ I = 0; $ I <$ nest; $ I ++ ){
  176. $ Template = preg_replace ("/[\ n \ r \ t] * \ {loop \ 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 +) \} [\ 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 replacement
  190. $ Template = preg_replace ("/\ {([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *) \}/s ",
  191. " ",
  192. $ Template
  193. );
  194. // Delete unnecessary spaces and line breaks in PHP code.
  195. $ Template = preg_replace ("/\? \> [\ N \ r] * \ <\? /S "," ", $ template );
  196. // Other replacement
  197. $ Template = preg_replace ("/\" (http )? [\ W \. \/:] + \? [^ \ "] +? & [^ \ "] +? \ "/E ",
  198. "Transamp ('\ 0 ')",
  199. $ Template
  200. );
  201. $ Template = preg_replace ("/\ ] *? Src = \ "(. + ?) \".*? \> \ S * \ <\/script \>/ise ",
  202. "Stripscriptamp ('\ 1 ')",
  203. $ Template
  204. );
  205. $ Template = preg_replace ("/[\ n \ r \ t] * \ {block \ s + ([a-zA-Z0-9 _] +) \} (. + ?) \ {\/Block \}/ies ",
  206. "Stripblock ('\ 1',' \ 2 ')",
  207. $ Template
  208. );
  209. // Add md5 and expiration verification
  210. $ Md5data = md5_file ($ tplfile );
  211. $ ExpireTime = time ();
  212. $ Template =" . "\ $ Template-> getInstance ()-> check ('$ file',' $ md5data ', $ expireTime );"
  213. . "?> \ R \ n $ template ";
  214. // Write the cached file
  215. $ Cachefile = $ this-> _ getCacheFile ($ file );
  216. $ Makepath = $ this-> _ makepath ($ cachefile );
  217. If ($ makepath! = True)
  218. $ This-> _ throwException ("the cache directory \" $ makepath \ "" cannot be created \"");
  219. File_put_contents ($ cachefile, $ template );
  220. }
  221. /**
  222. * Correct the path to the appropriate operating system format
  223. *
  224. * @ Param string $ path name
  225. * @ Return string
  226. */
  227. Protected function _ trimpath ($ path ){
  228. Return str_replace (array ('/', '\', '//', '\'), self: DIR_SEP, $ path );
  229. }
  230. /**
  231. * Get the template file name and path
  232. *
  233. * @ Param string $ file template file name
  234. * @ Return string
  235. */
  236. Protected function _ getTplFile ($ file ){
  237. Return $ this-> _ trimpath ($ this-> _ options ['Template _ dir']. self: DIR_SEP. $ file );
  238. }
  239. /**
  240. * Get the template cache file name and path
  241. *
  242. * @ Param string $ file template file name
  243. * @ Return string
  244. */
  245. Protected function _ getCacheFile ($ file ){
  246. $ File = preg_replace ('/\. [a-z0-9 \-_] + $/I', '. cache. php', $ file );
  247. Return $ this-> _ trimpath ($ this-> _ options ['cache _ dir']. self: DIR_SEP. $ file );
  248. }
  249. /**
  250. * Create a non-existent folder based on the specified path
  251. *
  252. * @ Param string $ path/folder name
  253. * @ Return string
  254. */
  255. Protected function _ makepath ($ path ){
  256. $ Dirs = explode (self: DIR_SEP, dirname ($ this-> _ trimpath ($ path )));
  257. $ Tmp = '';
  258. Foreach ($ dirs as $ dir ){
  259. $ Tmp. = $ dir. self: DIR_SEP;
  260. If (! File_exists ($ tmp )&&! @ Mkdir ($ tmp, 0777 ))
  261. Return $ tmp;
  262. }
  263. Return true;
  264. }
  265. /**
  266. * Throw an error message.
  267. *
  268. * @ Param string $ message
  269. * @ Return void
  270. */
  271. Protected function _ throwException ($ message ){
  272. Throw new Exception ($ message );
  273. }
  274. }
  275. ?>

  1. Template function file
  2. /**
  3. * Functions required for template replacement
  4. * Http://blog.qita.in
  5. */
  6. Function transamp ($ template ){
  7. $ Template = str_replace ('&', '&', $ template );
  8. $ Template = str_replace ('& amp;', '&', $ template );
  9. $ Template = str_replace ('\ "', '"', $ template );
  10. Return $ template;
  11. }
  12. Function stripvtags ($ expr, $ statement ){
  13. $ Expr = str_replace ("\\\" "," \ "", preg_replace ("/\< \? \= (\\\$. + ?) \? \>/S "," \ 1 ", $ expr ));
  14. $ Statement = str_replace ("\\\" "," \ "", $ statement );
  15. Return $ expr. $ statement;
  16. }
  17. Function addquote ($ var ){
  18. Return str_replace ("\" "," \ "", preg_replace ("/\ [([a-zA-Z0-9 _\-\. \ x7f-\ xff] +) \]/s "," ['\ 1'] ", $ var ));
  19. }
  20. Function stripscriptamp ($ s ){
  21. $ S = str_replace ('&', '&', $ s );
  22. Return"

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.