3 * Smarty Internal Plugin Compile Assign
4 * Compiles the {assign} tag
12 * Smarty Internal Plugin Compile Assign Class
15 * @subpackage Compiler
17 class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase
20 * Attribute definition: Overwrites base class.
23 * @see Smarty_Internal_CompileBase
25 public $option_flags = array('nocache', 'noscope');
32 public $valid_scopes = array('local' => Smarty::SCOPE_LOCAL, 'parent' => Smarty::SCOPE_PARENT,
33 'root' => Smarty::SCOPE_ROOT, 'global' => Smarty::SCOPE_GLOBAL,
34 'tpl_root' => Smarty::SCOPE_TPL_ROOT, 'smarty' => Smarty::SCOPE_SMARTY);
37 * Compiles code for the {assign} tag
39 * @param array $args array with attributes from parser
40 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
41 * @param array $parameter array with compilation parameter
43 * @return string compiled code
44 * @throws \SmartyCompilerException
46 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
48 // the following must be assigned at runtime because it will be overwritten in Smarty_Internal_Compile_Append
49 $this->required_attributes = array('var', 'value');
50 $this->shorttag_order = array('var', 'value');
51 $this->optional_attributes = array('scope');
52 $this->mapCache = array();
54 // check and get attributes
55 $_attr = $this->getAttributes($compiler, $args);
57 if ($_var = $compiler->getId($_attr[ 'var' ])) {
60 $_var = $_attr[ 'var' ];
62 if ($compiler->tag_nocache || $compiler->nocache) {
64 // create nocache var to make it know for further compiling
65 $compiler->setNocacheInVariable($_attr[ 'var' ]);
68 if ($_attr[ 'noscope' ]) {
71 $_scope = $compiler->convertScope($_attr, $this->valid_scopes);
75 if ($_nocache || $_scope) {
76 $_params .= ' ,' . var_export($_nocache, true);
79 $_params .= ' ,' . $_scope;
81 if (isset($parameter[ 'smarty_internal_index' ])) {
83 "<?php \$_tmp_array = isset(\$_smarty_tpl->tpl_vars[{$_var}]) ? \$_smarty_tpl->tpl_vars[{$_var}]->value : array();\n";
84 $output .= "if (!is_array(\$_tmp_array) || \$_tmp_array instanceof ArrayAccess) {\n";
85 $output .= "settype(\$_tmp_array, 'array');\n";
87 $output .= "\$_tmp_array{$parameter['smarty_internal_index']} = {$_attr['value']};\n";
88 $output .= "\$_smarty_tpl->_assignInScope({$_var}, \$_tmp_array{$_params});\n?>";
90 $output = "<?php \$_smarty_tpl->_assignInScope({$_var}, {$_attr['value']}{$_params});\n?>";