diff options
Diffstat (limited to 'phpBB/includes/template.inc')
-rw-r--r-- | phpBB/includes/template.inc | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/phpBB/includes/template.inc b/phpBB/includes/template.inc index a6d4e1a22b..b77a071acd 100644 --- a/phpBB/includes/template.inc +++ b/phpBB/includes/template.inc @@ -12,6 +12,12 @@ * This code is released under the GNU General Public Licence and used in * accordance with said licence. */ + + /** + * Some methods modified by Nathan Codding of the phpBB group for + * better performance - replacing preg_replace() with str_replace() + * where possible. + */ class Template { var $classname = "Template"; @@ -28,7 +34,7 @@ class Template { /* $varkeys[key] = "key"; $varvals[key] = "value"; */ var $varkeys = array(); var $varvals = array(); - + /* "remove" => remove undefined variables * "comment" => replace undefined variables with comments * "keep" => keep undefined variables @@ -108,10 +114,10 @@ class Template { $name = $handle; $str = $this->get_var($parent); - $reg = "/<!--\s+BEGIN $handle\s+-->(.*)\n\s*<!--\s+END $handle\s+-->/sm"; - preg_match_all($reg, $str, $m); + $reg = "/<!--\s+BEGIN $handle\s+-->(.*?)\n\s*<!--\s+END $handle\s+-->/sm"; + preg_match($reg, $str, $m); $str = preg_replace($reg, "{" . "$name}", $str); - $this->set_var($handle, $m[1][0]); + $this->set_var($handle, $m[1]); $this->set_var($parent, $str); } @@ -126,14 +132,14 @@ class Template { if (!is_array($varname)) { if (!empty($varname)) if ($this->debug) print "scalar: set *$varname* to *$value*<br>\n"; - $this->varkeys[$varname] = "/".$this->varname($varname)."/"; + $this->varkeys[$varname] = '{' . $varname . '}'; $this->varvals[$varname] = $value; } else { reset($varname); while(list($k, $v) = each($varname)) { if (!empty($k)) if ($this->debug) print "array: set *$k* to *$v*<br>\n"; - $this->varkeys[$k] = "/".$this->varname($k)."/"; + $this->varkeys[$k] = '{' . $k . '}'; $this->varvals[$k] = $v; } } @@ -148,8 +154,17 @@ class Template { return false; } - $str = $this->get_var($handle); - $str = @preg_replace($this->varkeys, $this->varvals, $str); + $str = $this->get_var($handle); + // This will break if $str is an array... Not sure if that ever + // actually happens, so we'll use this check for a while. + if (is_array($str)) die ("str is an array."); + + reset($this->varkeys); + while (list($k, $v) = each ($this->varkeys)) + { + $str = str_replace($this->varkeys[$k], $this->varvals[$k], $str); + } + return $str; } @@ -168,17 +183,25 @@ class Template { * handle: handle of template to substitute * append: append to target handle */ - function parse($target, $handle, $append = false) { - if (!is_array($handle)) { + function parse($target, $handle, $append = false) + { + if (!is_array($handle)) + { $str = $this->subst($handle); - if ($append) { + if ($append) + { $this->set_var($target, $this->get_var($target) . $str); - } else { + } + else + { $this->set_var($target, $str); } - } else { + } + else + { reset($handle); - while(list($i, $h) = each($handle)) { + while(list($i, $h) = each($handle)) + { $str = $this->subst($h); $this->set_var($target, $str); } @@ -300,7 +323,8 @@ class Template { function varname($varname) { return preg_quote("{".$varname."}"); } - + + /* private: loadfile(string $handle) * handle: load file defined by handle, if it is not loaded yet. */ |