smarty_internal_compile_assign.php 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * Compiles code for the {assign} tag
  20. *
  21. * @param array $args array with attributes from parser
  22. * @param object $compiler compiler object
  23. * @param array $parameter array with compilation parameter
  24. *
  25. * @return string compiled code
  26. */
  27. public function compile($args, $compiler, $parameter)
  28. {
  29. // the following must be assigned at runtime because it will be overwritten in Smarty_Internal_Compile_Append
  30. $this->required_attributes = array('var', 'value');
  31. $this->shorttag_order = array('var', 'value');
  32. $this->optional_attributes = array('scope');
  33. $_nocache = 'null';
  34. $_scope = Smarty::SCOPE_LOCAL;
  35. // check and get attributes
  36. $_attr = $this->getAttributes($compiler, $args);
  37. // nocache ?
  38. if ($compiler->tag_nocache || $compiler->nocache) {
  39. $_nocache = 'true';
  40. // create nocache var to make it know for further compiling
  41. if (isset($compiler->template->tpl_vars[trim($_attr['var'], "'")])) {
  42. $compiler->template->tpl_vars[trim($_attr['var'], "'")]->nocache = true;
  43. } else {
  44. $compiler->template->tpl_vars[trim($_attr['var'], "'")] = new Smarty_variable(null, true);
  45. }
  46. }
  47. // scope setup
  48. if (isset($_attr['scope'])) {
  49. $_attr['scope'] = trim($_attr['scope'], "'\"");
  50. if ($_attr['scope'] == 'parent') {
  51. $_scope = Smarty::SCOPE_PARENT;
  52. } elseif ($_attr['scope'] == 'root') {
  53. $_scope = Smarty::SCOPE_ROOT;
  54. } elseif ($_attr['scope'] == 'global') {
  55. $_scope = Smarty::SCOPE_GLOBAL;
  56. } else {
  57. $compiler->trigger_template_error('illegal value for "scope" attribute', $compiler->lex->taglineno);
  58. }
  59. }
  60. // compiled output
  61. if (isset($parameter['smarty_internal_index'])) {
  62. $output = "<?php \$_smarty_tpl->createLocalArrayVariable($_attr[var], $_nocache, $_scope);\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value$parameter[smarty_internal_index] = $_attr[value];";
  63. } else {
  64. // implement Smarty2's behaviour of variables assigned by reference
  65. if ($compiler->template->smarty instanceof SmartyBC) {
  66. $output = "<?php if (isset(\$_smarty_tpl->tpl_vars[$_attr[var]])) {\$_smarty_tpl->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
  67. $output .= "\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value = $_attr[value]; \$_smarty_tpl->tpl_vars[$_attr[var]]->nocache = $_nocache; \$_smarty_tpl->tpl_vars[$_attr[var]]->scope = $_scope;";
  68. $output .= "\n} else \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_variable($_attr[value], $_nocache, $_scope);";
  69. } else {
  70. $output = "<?php \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_variable($_attr[value], $_nocache, $_scope);";
  71. }
  72. }
  73. if ($_scope == Smarty::SCOPE_PARENT) {
  74. $output .= "\nif (\$_smarty_tpl->parent != null) \$_smarty_tpl->parent->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
  75. } elseif ($_scope == Smarty::SCOPE_ROOT || $_scope == Smarty::SCOPE_GLOBAL) {
  76. $output .= "\n\$_ptr = \$_smarty_tpl->parent; while (\$_ptr != null) {\$_ptr->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]]; \$_ptr = \$_ptr->parent; }";
  77. }
  78. if ($_scope == Smarty::SCOPE_GLOBAL) {
  79. $output .= "\nSmarty::\$global_tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
  80. }
  81. $output .= '?>';
  82. return $output;
  83. }
  84. }