From 60e5dda1b5e1be36a15c45ded2635340087a0b0f Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Fri, 2 Mar 2012 12:24:35 +0100 Subject: template proc: first optimization --- include/template_processor.php | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'include/template_processor.php') diff --git a/include/template_processor.php b/include/template_processor.php index b4d444e1c..a80a3997a 100755 --- a/include/template_processor.php +++ b/include/template_processor.php @@ -1,5 +1,5 @@ stack[] = array($this->r, $this->search, $this->replace, $this->nodes); + $this->stack[] = array($this->r, $this->nodes); } private function _pop_stack(){ - list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack); + list($this->r, $this->nodes) = array_pop($this->stack); } - private function _get_var($name){ - $keys = array_map('trim',explode(".",$name)); + private function _get_var($name, $retNoKey=false){ + $keys = array_map('trim',explode(".",$name)); + if ($retNoKey && !array_key_exists($keys[0], $this->r)) return KEY_NOT_EXISTS; $val = $this->r; foreach($keys as $k) { $val = (isset($val[$k]) ? $val[$k] : null); @@ -194,13 +195,24 @@ return str_replace($this->search,$this->replace, $str); }*/ + private function var_replace($s){ + $m = array(); + if (preg_match_all('/\$([a-zA-Z0-9-_]+\.*)+/', $s,$m)){ + foreach($m[0] as $var){ + $val = $this->_get_var($var, true); + if ($val!=KEY_NOT_EXISTS) + $s = str_replace($var, $val, $s); + } + } + return $s; + } public function replace($s, $r) { $this->r = $r; - $this->search = array(); - $this->replace = array(); + /*$this->search = array(); + $this->replace = array();*/ - $this->_build_replace($r, ""); + //$this->_build_replace($r, ""); #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s); $s = $this->_build_nodes($s); @@ -215,7 +227,8 @@ while($os!=$s && $count<10){ $os=$s; $count++; //$s = $this->_str_replace($s); - $s = str_replace($this->search, $this->replace, $s); + $s = $this->var_replace($s); + //$s = str_replace($this->search, $this->replace, $s); } return template_unescape($s); } -- cgit v1.2.3