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
- ? /**
- * Template class-parsing using Discuz template engine
- * 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 invocation method
- *
- * @static
- * @return Object Template
- */
- public static function getinstance () {
- if (!self:: $_instance instanceof Self)
- Self:: $_instance = new self ();
- Return self:: $_instance;
- }
- /**
- * Construction Method
- *
- * @return void
- */
- Private Function __construct () {
- $this-_options = Array (' template_dir ' = ' templates '. Self:: DIR_SEP,//directory where template files are located
- ' Cache_dir ' = ' templates '. Self:: dir_sep. ' Cache '. Self:: DIR_SEP,//cache file storage Directory
- ' Auto_update ' = false,//if the cache is regenerated when the template file changes
- ' Cache_lifetime ' = 0,//Cache life cycle (minutes), 0 for permanent
- );
- }
- /**
- * Set template parameter information
- *
- * @param array $options parameter
- * @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 values
- * @return void
- */
- Public function set ($name, $value) {
- Switch ($name) {
- Case ' Template_dir ':
- $value = $this-_trimpath ($value);
- if (!file_exists ($value))
- _throwexception, $this ("The specified template directory not found \" $value \ "");
- $this-_options[' template_dir ') = $value;
- Break
- Case ' Cache_dir ':
- $value = $this-_trimpath ($value);
- if (!file_exists ($value))
- _throwexception, $this ("The specified cache directory not found \" $value \ "");
- $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 method
- *
- * @see Template::set ()
- * @param string $name parameter name
- * @param mixed $value parameter values
- * @return void
- */
- Public Function __set ($name, $value) {
- $this, set ($name, $value);
- }
- /**
- * Get 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;
- }
- /**
- * Detects if the template file needs to update the cache
- *
- * @param string $file template file name
- * @param string $MD 5data template file MD5 checksum information
- * @param integer $md 5data template file expiry time check information
- * @return void
- */
- Public function Check ($file, $md 5data, $expireTime) {
- if ($this _options[' auto_update ') && md5_file ($this-_gettplfile ($file))! = $MD 5data)
- $this, Cache ($file);
- if ($this _options[' cache_lifetime ']! = 0 && (Time ()-$expireTime >= $this-_options[' Cache_lifetim E '] * 60))
- $this, Cache ($file);
- }
- /**
- * Caching of 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");
- }
- Get template Content
- $template = file_get_contents ($tplfile);
- Filter
- $template = Preg_replace ("/\<\!\-\-\{(. +?) \}\-\-\>/s "," {\\1} ", $template);
- Replace Language pack variables
- $template = Preg_replace ("/\{lang\s+ (. +?) \}/ies "," Languagevar (' \\1 ') ", $template);
- Replace PHP line break
- $template = Str_replace ("{LF}", " ", $template);
- Replace 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 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 loop function and conditional judgment statement
- $nest = 5;
- 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 (' , ' \\3 ' )",
- $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 (' \\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 substitution
- $template = Preg_replace ("/\{([a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*) \}/s",
- " ",
- $template
- );
- Remove extra spaces and line breaks in PHP code breaks
- $template = Preg_replace ("/\?\>[\n\r]*\<\? /S "," ", $template);
- Other replacements
- $template = Preg_replace ("/\" (http)? [ \w\.\/:]+\? [^\ "]+?&[^\"]+?\ "/E",
- "Transamp (' \\0 ')",
- $template
- );
- $template = Preg_replace ("/\";
- }
- function Stripblock ($var, $s) {
- $s = str_replace (' \ \ "', '" ', $s);
- $s = Preg_replace ("/<\?=\\\$ (. +?) \?>/"," {\$\\1} ", $s);
- Preg_match_all ("/<\?= (. +?) \?>/e ", $s, $constary);
- $constadd = ";
- $constary [1] = Array_unique ($constary [1]);
- foreach ($constary [1] as $const) {
- $constadd. = ' $__ '. $const. ' = '. $const. ';';
- }
- $s = Preg_replace ("/<\?= (. +?) \?>/"," {\$__\\1} ", $s);
- $s = str_replace ('?> ', "\n\$ $var. = <<
- $s = str_replace (' return ' );
- }
- ?>
Copy Code |