support merging appended vars

This commit is contained in:
mohrt
2003-02-20 16:39:36 +00:00
parent 15f982004c
commit f66f204ef8
3 changed files with 53 additions and 33 deletions

2
NEWS
View File

@@ -1,5 +1,5 @@
- fix cache groups with compile_id set (Monte) - fix cache groups with compile_id set (Monte)
- add support for appending key=>value vars (messju, Monte) - add support for merging appended vars (messju, Monte)
- change embedded variable logic to only recognize $foo and - change embedded variable logic to only recognize $foo and
$foo[0][bar] and $smarty.foo.bar syntax (Monte) $foo[0][bar] and $smarty.foo.bar syntax (Monte)
- allow null as function attribute value - allow null as function attribute value

View File

@@ -578,9 +578,8 @@ class Smarty
} }
} }
} else { } else {
if ($tpl_var != '') { if ($tpl_var != '')
$this->_tpl_vars[$tpl_var] = $value; $this->_tpl_vars[$tpl_var] = $value;
}
} }
} }
@@ -602,18 +601,21 @@ class Smarty
* @param array|string $tpl_var the template variable name(s) * @param array|string $tpl_var the template variable name(s)
* @param mixed $value the value to append * @param mixed $value the value to append
*/ */
function append($tpl_var, $value = null) function append($tpl_var, $value=null, $merge=false)
{ {
if (is_array($tpl_var)) { if (is_array($tpl_var)) {
foreach ($tpl_var as $key => $val) { // $tpl_var is an array, ignore $value
if ($key != '') { foreach ($tpl_var as $_key => $_val) {
if(!@is_array($this->_tpl_vars[$key])) { if ($_key != '') {
settype($this->_tpl_vars[$key],'array'); if(!@is_array($this->_tpl_vars[$_key])) {
settype($this->_tpl_vars[$_key],'array');
} }
if(@is_array($val)) { if($merge && is_array($_val)) {
$this->_tpl_vars[$key] = array_merge($this->_tpl_vars[$key],$val); foreach($_val as $_mkey => $_mval) {
$this->_tpl_vars[$_key][$_mkey] = $_mval;
}
} else { } else {
$this->_tpl_vars[$key][] = $val; $this->_tpl_vars[$_key][] = $_val;
} }
} }
} }
@@ -622,10 +624,12 @@ class Smarty
if(!@is_array($this->_tpl_vars[$tpl_var])) { if(!@is_array($this->_tpl_vars[$tpl_var])) {
settype($this->_tpl_vars[$tpl_var],'array'); settype($this->_tpl_vars[$tpl_var],'array');
} }
if(@is_array($value)) { if($merge && is_array($value)) {
$this->_tpl_vars[$tpl_var] = array_merge($this->_tpl_vars[$tpl_var],$value); foreach($value as $_mkey => $_mval) {
$this->_tpl_vars[$tpl_var][$_mkey] = $_mval;
}
} else { } else {
$this->_tpl_vars[$tpl_var][] = $value; $this->_tpl_vars[$tpl_var][] = $value;
} }
} }
} }
@@ -637,13 +641,19 @@ class Smarty
* @param string $tpl_var the template variable name * @param string $tpl_var the template variable name
* @param mixed $value the referenced value to append * @param mixed $value the referenced value to append
*/ */
function append_by_ref($tpl_var, &$value) function append_by_ref($tpl_var, &$value, $merge=false)
{ {
if ($tpl_var != '' && isset($value)) { if ($tpl_var != '' && isset($value)) {
if(!@is_array($this->_tpl_vars[$tpl_var])) { if(!@is_array($this->_tpl_vars[$tpl_var])) {
settype($this->_tpl_vars[$tpl_var],'array'); settype($this->_tpl_vars[$tpl_var],'array');
}
if ($merge && is_array($value)) {
foreach($value as $_key => $_val) {
$this->_tpl_vars[$tpl_var][$_key] = &$value[$_key];
}
} else {
$this->_tpl_vars[$tpl_var][] = &$value;
} }
$this->_tpl_vars[$tpl_var][] = &$value;
} }
} }

View File

@@ -578,9 +578,8 @@ class Smarty
} }
} }
} else { } else {
if ($tpl_var != '') { if ($tpl_var != '')
$this->_tpl_vars[$tpl_var] = $value; $this->_tpl_vars[$tpl_var] = $value;
}
} }
} }
@@ -602,18 +601,21 @@ class Smarty
* @param array|string $tpl_var the template variable name(s) * @param array|string $tpl_var the template variable name(s)
* @param mixed $value the value to append * @param mixed $value the value to append
*/ */
function append($tpl_var, $value = null) function append($tpl_var, $value=null, $merge=false)
{ {
if (is_array($tpl_var)) { if (is_array($tpl_var)) {
foreach ($tpl_var as $key => $val) { // $tpl_var is an array, ignore $value
if ($key != '') { foreach ($tpl_var as $_key => $_val) {
if(!@is_array($this->_tpl_vars[$key])) { if ($_key != '') {
settype($this->_tpl_vars[$key],'array'); if(!@is_array($this->_tpl_vars[$_key])) {
settype($this->_tpl_vars[$_key],'array');
} }
if(@is_array($val)) { if($merge && is_array($_val)) {
$this->_tpl_vars[$key] = array_merge($this->_tpl_vars[$key],$val); foreach($_val as $_mkey => $_mval) {
$this->_tpl_vars[$_key][$_mkey] = $_mval;
}
} else { } else {
$this->_tpl_vars[$key][] = $val; $this->_tpl_vars[$_key][] = $_val;
} }
} }
} }
@@ -622,10 +624,12 @@ class Smarty
if(!@is_array($this->_tpl_vars[$tpl_var])) { if(!@is_array($this->_tpl_vars[$tpl_var])) {
settype($this->_tpl_vars[$tpl_var],'array'); settype($this->_tpl_vars[$tpl_var],'array');
} }
if(@is_array($value)) { if($merge && is_array($value)) {
$this->_tpl_vars[$tpl_var] = array_merge($this->_tpl_vars[$tpl_var],$value); foreach($value as $_mkey => $_mval) {
$this->_tpl_vars[$tpl_var][$_mkey] = $_mval;
}
} else { } else {
$this->_tpl_vars[$tpl_var][] = $value; $this->_tpl_vars[$tpl_var][] = $value;
} }
} }
} }
@@ -637,13 +641,19 @@ class Smarty
* @param string $tpl_var the template variable name * @param string $tpl_var the template variable name
* @param mixed $value the referenced value to append * @param mixed $value the referenced value to append
*/ */
function append_by_ref($tpl_var, &$value) function append_by_ref($tpl_var, &$value, $merge=false)
{ {
if ($tpl_var != '' && isset($value)) { if ($tpl_var != '' && isset($value)) {
if(!@is_array($this->_tpl_vars[$tpl_var])) { if(!@is_array($this->_tpl_vars[$tpl_var])) {
settype($this->_tpl_vars[$tpl_var],'array'); settype($this->_tpl_vars[$tpl_var],'array');
}
if ($merge && is_array($value)) {
foreach($value as $_key => $_val) {
$this->_tpl_vars[$tpl_var][$_key] = &$value[$_key];
}
} else {
$this->_tpl_vars[$tpl_var][] = &$value;
} }
$this->_tpl_vars[$tpl_var][] = &$value;
} }
} }