1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-31 22:10:45 +02:00

Performance hacks in template.inc - let me know if stuff breaks.

git-svn-id: file:///svn/phpbb/trunk@96 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
natec
2001-03-10 03:42:46 +00:00
parent 66bc8a309b
commit afae95b90f
3 changed files with 414 additions and 17 deletions

View File

@@ -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.
*/