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
- ? /**
- * Template class-use the Discuz template engine for parsing
- * Http://blog.qita.in
- */
- Require_once (DIR_ROOT. '/../function/template. func. php ');
- Class Template {
- Const DIR_SEP = DIRECTORY_SEPARATOR;
- /**
- * Template instance
- *
- * @ Staticvar
- * @ Var object Template
- */
- Protected static $ _ instance;
- /**
- * Template parameter information
- *
- * @ Var array
- */
- Protected $ _ options = array ();
- /**
- * Single-piece mode call method
- *
- * @ Static
- * @ Return object Template
- */
- Public static function getInstance (){
- If (! Self: $ _ instance instanceof self)
- Self: $ _ instance = new self ();
- Return self: $ _ instance;
- }
- /**
- * Constructor
- *
- * @ Return void
- */
- Private function _ construct (){
- $ This-> _ options = array ('Template _ dir' => 'templates'. self: DIR_SEP, // Directory of the template file
- 'Cache _ dir' => 'templates'. self: DIR_SEP. 'cache'. self: DIR_SEP, // cache file storage directory
- 'Auto _ update' => false, // whether to regenerate the cache when the template file is changed
- 'Cache _ lifetime' => 0, // cache lifecycle (minutes), 0 indicates permanent
- );
- }
- /**
- * Set template parameter information
- *
- * @ Param array $ options parameter array
- * @ Return void
- */
- Public function setOptions (array $ options ){
- Foreach ($ options as $ name => $ value)
- $ This-> set ($ name, $ value );
- }
- /**
- * Set template parameters
- *
- * @ Param string $ name parameter name
- * @ Param mixed $ value parameter value
- * @ Return void
- */
- Public function set ($ name, $ value ){
- Switch ($ name ){
- Case 'Template _ dir ':
- $ Value = $ this-> _ trimpath ($ value );
- If (! File_exists ($ value ))
- $ This-> _ throwException ("The specified template directory \" $ value \ "" is not found \"");
- $ This-> _ options ['Template _ dir'] = $ value;
- Break;
- Case 'cache _ dir ':
- $ Value = $ this-> _ trimpath ($ value );
- If (! File_exists ($ value ))
- $ This-> _ throwException ("The specified cache directory \" $ value \ "" is not found \"");
- $ This-> _ options ['cache _ dir'] = $ value;
- Break;
- Case 'auto _ update ':
- $ This-> _ options ['auto _ update'] = (boolean) $ value;
- Break;
- Case 'cache _ lifetime ':
- $ This-> _ options ['cache _ lifetime'] = (float) $ value;
- Break;
- Default:
- $ This-> _ throwException ("unknown template configuration options \" $ name \"");
- }
- }
- /**
- * Set template parameters by magic
- *
- * @ See Template: set ()
- * @ Param string $ name parameter name
- * @ Param mixed $ value parameter value
- * @ Return void
- */
- Public function _ set ($ name, $ value ){
- $ This-> set ($ name, $ value );
- }
- /**
- * Getting Template Files
- *
- * @ Param string $ file template file name
- * @ Return string
- */
- Public function getfile ($ file ){
- $ Cachefile = $ this-> _ getCacheFile ($ file );
- If (! File_exists ($ cachefile ))
- $ This-> cache ($ file );
- Return $ cachefile;
- }
- /**
- * Checks whether the template file needs to update the cache.
- *
- * @ Param string $ file template file name
- * @ Param string $ md5 verification information of the md5data template file
- * @ Param integer $ md5data template file expiration time verification information
- * @ Return void
- */
- Public function check ($ file, $ md5data, $ expireTime ){
- If ($ this-> _ options ['auto _ update'] & md5_file ($ this-> _ getTplFile ($ file ))! = $ Md5data)
- $ This-> cache ($ file );
- If ($ this-> _ options ['cache _ lifetime']! = 0 & (time ()-$ expireTime >=$ this-> _ options ['cache _ lifetime'] * 60 ))
- $ This-> cache ($ file );
- }
- /**
- * Cache template files
- *
- * @ Param string $ file template file name
- * @ Return void
- */
- Public function cache ($ file ){
- $ Tplfile = $ this-> _ getTplFile ($ file );
- If (! Is_readable ($ tplfile )){
- $ This-> _ throwException ("template file \" $ tplfile \ "not found or cannot be opened ");
- }
- // Obtain the template content
- $ Template = file_get_contents ($ tplfile );
- // Filter
- $ Template = preg_replace ("/\ <\! \-\ {(. + ?) \}\-\>/S "," {\\ 1} ", $ template );
- // Replace language pack variables
- // $ Template = preg_replace ("// \ {lang \ s + (. + ?) \}/Ies "," your agevar ('\ 1') ", $ template );
- // Replace the PHP line break
- $ Template = str_replace ("{LF }"," ", $ Template );
- // Replace the direct variable output
- $ VarRegexp = "(\ $ [a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *)"
- . "(\ [A-zA-Z0-9 _ \-\. \" \ '\ [\] \ $ \ x7f-\ xff] + \]) *) ";
- $ Template = preg_replace ("/\ {(\ $[a-zA-Z0-9 _ \ [\] \ '\" \ $ \. \ x7f-\ xff] +) \}/s "," ", $ Template );
- $ Template = preg_replace ("/$ varRegexp/es", "addquote (' ') ", $ Template );
- $ Template = preg_replace ("/\ <\? \=\< \? \=$ VarRegexp \? \> \? \>/Es "," addquote (' ') ", $ Template );
- // Replace the template load command
- $ Template = preg_replace ("/[\ n \ r \ t] * \ {template \ s + ([a-z0-9 _] +) \} [\ n \ r \ t] */is ",
- "\ R \ n Getfile ('\ 1');?> \ R \ n ",
- $ Template
- );
- $ Template = preg_replace ("/[\ n \ r \ t] * \ {template \ s + (. + ?) \} [\ N \ r \ t] */is ",
- "\ R \ n Getfile (\ 1);?> \ R \ n ",
- $ Template
- );
- // Replace a specific function
- $ Template = preg_replace ("/[\ n \ r \ t] * \ {eval \ s + (. + ?) \} [\ N \ r \ t] */ies ",
- "Stripvtags (' ','')",
- $ Template
- );
- $ Template = preg_replace ("/[\ n \ r \ t] * \ {echo \ s + (. + ?) \} [\ N \ r \ t] */ies ",
- "Stripvtags (' ','')",
- $ Template
- );
- $ Template = preg_replace ("/([\ n \ r \ t] *) \ {elseif \ s + (. + ?) \} ([\ N \ r \ t] *)/ies ",
- "Stripvtags ('\ 1 \ 3 ','')",
- $ Template
- );
- $ Template = preg_replace ("/([\ n \ r \ t] *) \ {else \} ([\ n \ r \ t] *)/is ",
- "\ 1 \ 2 ",
- $ Template
- );
- // Replace the cyclic function and condition judgment statement
- $ Nest = 5;
- For ($ I = 0; $ I <$ nest; $ I ++ ){
- $ Template = preg_replace ("/[\ n \ r \ t] * \ {loop \ s + (\ S +) \} [\ n \ r] * (. + ?) [\ N \ r] * \ {\/loop \} [\ n \ r \ t] */ies ",
- "Stripvtags (' ',' \ 3 ')",
- $ Template
- );
- $ Template = preg_replace ("/[\ n \ r \ t] * \ {loop \ s + (\ S +) \ s + (\ S +) \} [\ n \ r \ t] * (. + ?) [\ N \ r \ t] * \ {\/loop \} [\ n \ r \ t] */ies ",
- "Stripvtags (' \ 3) {?> ',' \ 4 ')",
- $ Template
- );
- $ Template = preg_replace ("/([\ n \ r \ t] *) \ {if \ s + (. + ?) \} ([\ N \ r] *) (. + ?) ([\ N \ r] *) \ {\/if \} ([\ n \ r \ t] *)/ies ",
- "Stripvtags ('\ 1 \ 3', '\ 4 \ 5 \ 6 ')",
- $ Template
- );
- }
- // Constant replacement
- $ Template = preg_replace ("/\ {([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *) \}/s ",
- " ",
- $ Template
- );
- // Delete unnecessary spaces and line breaks in PHP code.
- $ Template = preg_replace ("/\? \> [\ N \ r] * \ <\? /S "," ", $ template );
- // Other replacement
- $ Template = preg_replace ("/\" (http )? [\ W \. \/:] + \? [^ \ "] +? & [^ \ "] +? \ "/E ",
- "Transamp ('\ 0 ')",
- $ Template
- );
- $ Template = preg_replace ("/\ ] *? Src = \ "(. + ?) \".*? \> \ S * \ <\/script \>/ise ",
- "Stripscriptamp ('\ 1 ')",
- $ Template
- );
- $ Template = preg_replace ("/[\ n \ r \ t] * \ {block \ s + ([a-zA-Z0-9 _] +) \} (. + ?) \ {\/Block \}/ies ",
- "Stripblock ('\ 1',' \ 2 ')",
- $ Template
- );
- // Add md5 and expiration verification
- $ Md5data = md5_file ($ tplfile );
- $ ExpireTime = time ();
- $ Template =" . "\ $ Template-> getInstance ()-> check ('$ file',' $ md5data ', $ expireTime );"
- . "?> \ R \ n $ template ";
- // Write the cached file
- $ Cachefile = $ this-> _ getCacheFile ($ file );
- $ Makepath = $ this-> _ makepath ($ cachefile );
- If ($ makepath! = True)
- $ This-> _ throwException ("the cache directory \" $ makepath \ "" cannot be created \"");
- File_put_contents ($ cachefile, $ template );
- }
- /**
- * Correct the path to the appropriate operating system format
- *
- * @ Param string $ path name
- * @ Return string
- */
- Protected function _ trimpath ($ path ){
- Return str_replace (array ('/', '\', '//', '\'), self: DIR_SEP, $ path );
- }
- /**
- * Get the template file name and path
- *
- * @ Param string $ file template file name
- * @ Return string
- */
- Protected function _ getTplFile ($ file ){
- Return $ this-> _ trimpath ($ this-> _ options ['Template _ dir']. self: DIR_SEP. $ file );
- }
- /**
- * Get the template cache file name and path
- *
- * @ Param string $ file template file name
- * @ Return string
- */
- Protected function _ getCacheFile ($ file ){
- $ File = preg_replace ('/\. [a-z0-9 \-_] + $/I', '. cache. php', $ file );
- Return $ this-> _ trimpath ($ this-> _ options ['cache _ dir']. self: DIR_SEP. $ file );
- }
- /**
- * Create a non-existent folder based on the specified path
- *
- * @ Param string $ path/folder name
- * @ Return string
- */
- Protected function _ makepath ($ path ){
- $ Dirs = explode (self: DIR_SEP, dirname ($ this-> _ trimpath ($ path )));
- $ Tmp = '';
- Foreach ($ dirs as $ dir ){
- $ Tmp. = $ dir. self: DIR_SEP;
- If (! File_exists ($ tmp )&&! @ Mkdir ($ tmp, 0777 ))
- Return $ tmp;
- }
- Return true;
- }
- /**
- * Throw an error message.
- *
- * @ Param string $ message
- * @ Return void
- */
- Protected function _ throwException ($ message ){
- Throw new Exception ($ message );
- }
- }
- ?>
- Template function file
- /**
- * Functions required for template replacement
- * Http://blog.qita.in
- */
- Function transamp ($ template ){
- $ Template = str_replace ('&', '&', $ template );
- $ Template = str_replace ('& amp;', '&', $ template );
- $ Template = str_replace ('\ "', '"', $ template );
- Return $ template;
- }
- Function stripvtags ($ expr, $ statement ){
- $ Expr = str_replace ("\\\" "," \ "", preg_replace ("/\< \? \= (\\\$. + ?) \? \>/S "," \ 1 ", $ expr ));
- $ Statement = str_replace ("\\\" "," \ "", $ statement );
- Return $ expr. $ statement;
- }
- Function addquote ($ var ){
- Return str_replace ("\" "," \ "", preg_replace ("/\ [([a-zA-Z0-9 _\-\. \ x7f-\ xff] +) \]/s "," ['\ 1'] ", $ var ));
- }
- Function stripscriptamp ($ s ){
- $ S = str_replace ('&', '&', $ s );
- Return"
|