You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_compile_assign.php 4.3KB

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