Discuz’s template engine is a relatively good template engine class. I found it on the Internet a long time ago. Visually, this Discuz’s template engine should be very old. It is a version before DZ7.2. I also use it very smoothly. Share it. Download this template class. There are two files. A template class, a function that needs to be used in template replacement Original address: http://blog.qita.in
- ?/**
- * Template class - parsed 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();
-
- /**
- * Singleton mode calling 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, // The directory where the template file is located
- 'cache_dir' => 'templates' . self :: DIR_SEP . 'cache' . self :: DIR_SEP, // Directory where cache files are stored
- 'auto_update' => false, // Whether to regenerate the cache when the template file is changed
- 'cache_lifetime' => 0, // Cache life cycle ( minutes), 0 means 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"" was 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"" was 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 option "$name"");
- }
- }
-
- /**
- * Set template parameters through magic method
- *
- * @see Template::set()
- * @param string $name parameter name
- * @param mixed $value parameter value
- * @return void
- */
- public function __set($name, $value) {
- $this -> set( $name, $value);
- }
-
- /**
- * Get template file
- *
- * @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;
- }
-
- /**
- * Check whether the template file needs to update the cache
- *
- * @param string $file template file name
- * @param string $md5data template file md5 verification information
- * @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" was not found or could not be opened");
- }
- // Get template content
- $template = file_get_contents($tplfile);
- // Filter/s", "{\1}", $template);
- // Replace language pack variables
- // $template = preg_replace("/{langs+(.+?)}/ies", "languagevar('\1')", $template);
- // Replace PHP newline character
- $template = str_replace("{LF}", "="\n"?>", $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", "=\1?>", $template);
- $template = preg_replace(" /$varRegexp/es", "addquote('=\1?>')", $template);
- $template = preg_replace("/==$varRegexp?>? >/es", "addquote('=\1?>')", $template);
- // Replace template loading command
- $template = preg_replace("/[nrt]*{templates+( [a-z0-9_]+)}[nrt]*/is",
- "rn include($template->getfile('\1')); ?>rn",
- $template
- ) ;
- $template = preg_replace("/[nrt]*{templates+(.+?)}[nrt]*/is",
- "rn include($template->getfile(\1)); ?> ;rn",
- $template
- );
- // Replace specific function
- $template = preg_replace("/[nrt]*{evals+(.+?)}[nrt]*/ies",
- "stripvtags('< ;? \1 ?>','')",
- $template
- );
- $template = preg_replace("/[nrt]*{echos+(.+?)}[nrt]*/ies",
- " stripvtags(' echo \1; ?>','')",
- $template
- );
- $template = preg_replace("/([nrt]*){elseifs+(.+?)}([ nrt]*)/ies",
- "stripvtags('\1 } elseif(\2) { ?>\3','')",
- $template
- );
- $template = preg_replace("/ ([nrt]*){else}([nrt]*)/is",
- "\1 } else { ?>\2",
- $template
- );
- // Replace loop function and conditional judgment Statement
- $nest = 5;
- for ($i = 0; $i $template = preg_replace("/[nrt]*{loops+(S+)s+(S+)}[nr ]*(.+?)[nr]*{/loop}[nrt]*/ies",
- "stripvtags(' if(is_array(\1)) { foreach(\1 as \2) { ? >','\3 } } ?>')",
- $template
- );
- $template = preg_replace("/[nrt]*{loops+(S+)s+(S+)s+(S+)} [nrt]*(.+?)[nrt]*{/loop}[nrt]*/ies",
- "stripvtags(' if(is_array(\1)) { foreach(\1 as \2 = > \3) { ?>','\4 } } ?>')",
- $template
- );
- $template = preg_replace("/([nrt]*){ifs+(.+ ?)}([nr]*)(.+?)([nr]*){/if}([nrt]*)/ies",
- "stripvtags('\1 if(\2) { ? >\3','\4\5 } ?>\6')",
- $template
- );
- }
- // Constant replacement
- $template = preg_replace("/{([a-zA -Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)}/s",
- "=\1?>",
- $template
- );
- // Delete PHP code break Extra spaces and line breaks
- $template = preg_replace("/ ?>[nr]* /s", " ", $template);
- // Other replacements
- $template = preg_replace("/"(http )?[w./:]+?[^"]+?&[^"]+?"/e",
- "transamp('\0')",
- $template
- );
- $template = preg_replace ("/<script>]*?src="(.+?)".*?>s*</script>/ise",
- "stripscriptamp('\1')",
- $template
- );
- $template = preg_replace("/[nrt]*{blocks+([a-zA-Z0-9_]+)}(.+?){/block}/ies",
- "stripblock(' \1', '\2')",
- $template
- );
- // Add md5 and expiration check
- $md5data = md5_file($tplfile);
- $expireTime = time();
- $template = "< ;? if (!class_exists('template')) die('Access Denied');"
- . "$template->getInstance()->check('$file', '$md5data', $expireTime) ;"
- . "?>rn$template";
- // Write cache file
- $cachefile = $this -> _getCacheFile($file);
- $makepath = $this -> _makepath($cachefile);
- if ($makepath !== true)
- $this -> _throwException("Unable to create cache directory "$makepath"");
- file_put_contents($cachefile, $template);
- }
-
- /**
- * Correct the path to a form suitable for the operating system
- *
- * @param string $path 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-existing folder based on the specified path
- *
- * @param string $path 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);
- }
- }
-
- ?>
-
-
复制代码
- 模板函数文件
-
/**
- * Functions needed for template replacement
- * http://blog.qita.in
- */
-
- function transamp($template) {
- $template = str_replace('&', '&', $template);
- $template = str_replace('&', '&', $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 "";
- }
-
- 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('', "nEOF;n", $s);
- return "";
- }
- ?>
复制代码
|