1<?php 2/** 3 * Smarty Internal Plugin Compile Assign 4 * Compiles the {assign} tag 5 * 6 * @package Smarty 7 * @subpackage Compiler 8 * @author Uwe Tews 9 */ 10 11/** 12 * Smarty Internal Plugin Compile Assign Class 13 * 14 * @package Smarty 15 * @subpackage Compiler 16 */ 17class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase 18{ 19 /** 20 * Valid scope names 21 * 22 * @var array 23 */ 24 public $valid_scopes = array('local' => true, 'parent' => true, 'root' => true, 'global' => true, 25 'smarty' => true, 'tpl_root' => true); 26 27 /** 28 * Compiles code for the {assign} tag 29 * 30 * @param array $args array with attributes from parser 31 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object 32 * @param array $parameter array with compilation parameter 33 * 34 * @return string compiled code 35 * @throws \SmartyCompilerException 36 */ 37 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) 38 { 39 // the following must be assigned at runtime because it will be overwritten in Smarty_Internal_Compile_Append 40 $this->required_attributes = array('var', 'value'); 41 $this->shorttag_order = array('var', 'value'); 42 $this->optional_attributes = array('scope', 'bubble_up'); 43 $_nocache = 'null'; 44 // check and get attributes 45 $_attr = $this->getAttributes($compiler, $args); 46 // nocache ? 47 if ($compiler->tag_nocache || $compiler->nocache) { 48 $_nocache = 'true'; 49 // create nocache var to make it know for further compiling 50 if (isset($compiler->template->tpl_vars[trim($_attr['var'], "'")])) { 51 $compiler->template->tpl_vars[trim($_attr['var'], "'")]->nocache = true; 52 } else { 53 $compiler->template->tpl_vars[trim($_attr['var'], "'")] = new Smarty_Variable(null, true); 54 } 55 } 56 // scope setup 57 $_scope = Smarty::SCOPE_LOCAL; 58 if (isset($_attr['scope'])) { 59 $_attr['scope'] = trim($_attr['scope'], "'\""); 60 if (!isset($this->valid_scopes[$_attr['scope']])) { 61 $compiler->trigger_template_error("illegal value '{$_attr['scope']}' for \"scope\" attribute", null, true); 62 } 63 if ($_attr['scope'] != 'local') { 64 if ($_attr['scope'] == 'parent') { 65 $_scope = Smarty::SCOPE_PARENT; 66 } elseif ($_attr['scope'] == 'root') { 67 $_scope = Smarty::SCOPE_ROOT; 68 } elseif ($_attr['scope'] == 'global') { 69 $_scope = Smarty::SCOPE_GLOBAL; 70 } elseif ($_attr['scope'] == 'smarty') { 71 $_scope = Smarty::SCOPE_SMARTY; 72 } elseif ($_attr['scope'] == 'tpl_root') { 73 $_scope = Smarty::SCOPE_TPL_ROOT; 74 } 75 $_scope += (isset($_attr['bubble_up']) && $_attr['bubble_up'] == 'false') ? 0 : Smarty::SCOPE_BUBBLE_UP; 76 } 77 } 78 // compiled output 79 if (isset($parameter['smarty_internal_index'])) { 80 $output = 81 "<?php \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, $_attr[var], $_nocache);\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value$parameter[smarty_internal_index] = $_attr[value];"; 82 } else { 83 // implement Smarty2's behaviour of variables assigned by reference 84 if ($compiler->template->smarty instanceof SmartyBC) { 85 $output = 86 "<?php if (isset(\$_smarty_tpl->tpl_vars[$_attr[var]])) {\$_smarty_tpl->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];"; 87 $output .= "\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value = $_attr[value]; \$_smarty_tpl->tpl_vars[$_attr[var]]->nocache = $_nocache;"; 88 $output .= "\n} else \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_Variable($_attr[value], $_nocache);"; 89 } else { 90 $output = "<?php \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_Variable($_attr[value], $_nocache);"; 91 } 92 } 93 $output .= "\n\$_smarty_tpl->ext->_updateScope->updateScope(\$_smarty_tpl, $_attr[var], $_scope);"; 94 $output .= '?>'; 95 96 return $output; 97 } 98} 99