1<?php
2
3/**
4 * Smarty Method AppendByRef
5 *
6 * Smarty::appendByRef() method
7 *
8 * @package    Smarty
9 * @subpackage PluginsInternal
10 * @author     Uwe Tews
11 */
12class Smarty_Internal_Method_AppendByRef
13{
14    /**
15     * appends values to template variables by reference
16     *
17     * @api  Smarty::appendByRef()
18     * @link http://www.smarty.net/docs/en/api.append.by.ref.tpl
19     *
20     * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
21     * @param string                                                  $tpl_var the template variable name
22     * @param mixed                                                   &$value  the referenced value to append
23     * @param bool                                                    $merge   flag if array elements shall be merged
24     *
25     * @return \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty
26     */
27    public static function appendByRef(Smarty_Internal_Data $data, $tpl_var, &$value, $merge = false)
28    {
29        if ($tpl_var !== '' && isset($value)) {
30            if (!isset($data->tpl_vars[ $tpl_var ])) {
31                $data->tpl_vars[ $tpl_var ] = new Smarty_Variable();
32            }
33            if (!is_array($data->tpl_vars[ $tpl_var ]->value)) {
34                settype($data->tpl_vars[ $tpl_var ]->value, 'array');
35            }
36            if ($merge && is_array($value)) {
37                foreach ($value as $_key => $_val) {
38                    $data->tpl_vars[ $tpl_var ]->value[ $_key ] = &$value[ $_key ];
39                }
40            } else {
41                $data->tpl_vars[ $tpl_var ]->value[] = &$value;
42            }
43            if ($data->_isTplObj() && $data->scope) {
44                $data->ext->_updateScope->_updateScope($data, $tpl_var);
45            }
46        }
47        return $data;
48    }
49}
50