From 212a27346eb3fb514877477a334bf44638d435be Mon Sep 17 00:00:00 2001 From: mohrt Date: Wed, 19 Feb 2003 16:40:13 +0000 Subject: [PATCH] support appending key=>val pairs --- NEWS | 1 + Smarty.class.php | 29 ++++++++++++++++++++--------- libs/Smarty.class.php | 29 ++++++++++++++++++++--------- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/NEWS b/NEWS index 65f22778..1804a9bb 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,4 @@ + - add support for appending key=>value vars (messju, Monte) - change embedded variable logic to only recognize $foo and $foo[0][bar] syntax (Monte) - allow null as function attribute value diff --git a/Smarty.class.php b/Smarty.class.php index b819c180..acc7bfdf 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -600,16 +600,20 @@ class Smarty * * @param array|string $tpl_var the template variable name(s) * @param mixed $value the value to append + * @param mixed $key the key of value to append */ - function append($tpl_var, $value = null) + function append($tpl_var, $value = null, $key=null) { if (is_array($tpl_var)) { - foreach ($tpl_var as $key => $val) { - if ($key != '') { - if(!@is_array($this->_tpl_vars[$key])) { - settype($this->_tpl_vars[$key],'array'); + foreach ($tpl_var as $name=>$val) { + if ($var_name != '') { + if(!@is_array($this->_tpl_vars[$name])) { + settype($this->_tpl_vars[$name],'array'); } - $this->_tpl_vars[$key][] = $val; + if (isset($key)) + $this->_tpl_vars[$name][$key] = $val; + else + $this->_tpl_vars[$name][] = $val; } } } else { @@ -617,7 +621,10 @@ class Smarty if(!@is_array($this->_tpl_vars[$tpl_var])) { settype($this->_tpl_vars[$tpl_var],'array'); } - $this->_tpl_vars[$tpl_var][] = $value; + if (isset($key)) + $this->_tpl_vars[$tpl_var][$key] = $value; + else + $this->_tpl_vars[$tpl_var][] = $value; } } } @@ -627,14 +634,18 @@ class Smarty * * @param string $tpl_var the template variable name * @param mixed $value the referenced value to append + * @param mixed $key the key of value to append */ - function append_by_ref($tpl_var, &$value) + function append_by_ref($tpl_var, &$value, $key=null) { if ($tpl_var != '' && isset($value)) { if(!@is_array($this->_tpl_vars[$tpl_var])) { settype($this->_tpl_vars[$tpl_var],'array'); } - $this->_tpl_vars[$tpl_var][] = &$value; + if (isset($key)) + $this->_tpl_vars[$tpl_var][$key] = &$value; + else + $this->_tpl_vars[$tpl_var][] = &$value; } } diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index b819c180..acc7bfdf 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -600,16 +600,20 @@ class Smarty * * @param array|string $tpl_var the template variable name(s) * @param mixed $value the value to append + * @param mixed $key the key of value to append */ - function append($tpl_var, $value = null) + function append($tpl_var, $value = null, $key=null) { if (is_array($tpl_var)) { - foreach ($tpl_var as $key => $val) { - if ($key != '') { - if(!@is_array($this->_tpl_vars[$key])) { - settype($this->_tpl_vars[$key],'array'); + foreach ($tpl_var as $name=>$val) { + if ($var_name != '') { + if(!@is_array($this->_tpl_vars[$name])) { + settype($this->_tpl_vars[$name],'array'); } - $this->_tpl_vars[$key][] = $val; + if (isset($key)) + $this->_tpl_vars[$name][$key] = $val; + else + $this->_tpl_vars[$name][] = $val; } } } else { @@ -617,7 +621,10 @@ class Smarty if(!@is_array($this->_tpl_vars[$tpl_var])) { settype($this->_tpl_vars[$tpl_var],'array'); } - $this->_tpl_vars[$tpl_var][] = $value; + if (isset($key)) + $this->_tpl_vars[$tpl_var][$key] = $value; + else + $this->_tpl_vars[$tpl_var][] = $value; } } } @@ -627,14 +634,18 @@ class Smarty * * @param string $tpl_var the template variable name * @param mixed $value the referenced value to append + * @param mixed $key the key of value to append */ - function append_by_ref($tpl_var, &$value) + function append_by_ref($tpl_var, &$value, $key=null) { if ($tpl_var != '' && isset($value)) { if(!@is_array($this->_tpl_vars[$tpl_var])) { settype($this->_tpl_vars[$tpl_var],'array'); } - $this->_tpl_vars[$tpl_var][] = &$value; + if (isset($key)) + $this->_tpl_vars[$tpl_var][$key] = &$value; + else + $this->_tpl_vars[$tpl_var][] = &$value; } }