smarty_internal_compile_assign.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Assign
  4. *
  5. * Compiles the {assign} tag
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Assign Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase {
  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. * @return string compiled code
  25. */
  26. public function compile($args, $compiler, $parameter)
  27. {
  28. // the following must be assigned at runtime because it will be overwritten in Smarty_Internal_Compile_Append
  29. $this->required_attributes = array('var', 'value');
  30. $this->shorttag_order = array('var', 'value');
  31. $this->optional_attributes = array('scope');
  32. $_nocache = 'null';
  33. $_scope = Smarty::SCOPE_LOCAL;
  34. // check and get attributes
  35. $_attr = $this->getAttributes($compiler, $args);
  36. // nocache ?
  37. if ($compiler->tag_nocache || $compiler->nocache) {
  38. $_nocache = 'true';
  39. // create nocache var to make it know for further compiling
  40. $compiler->template->tpl_vars[trim($_attr['var'], "'")] = new Smarty_variable(null, true);
  41. }
  42. // scope setup
  43. if (isset($_attr['scope'])) {
  44. $_attr['scope'] = trim($_attr['scope'], "'\"");
  45. if ($_attr['scope'] == 'parent') {
  46. $_scope = Smarty::SCOPE_PARENT;
  47. } elseif ($_attr['scope'] == 'root') {
  48. $_scope = Smarty::SCOPE_ROOT;
  49. } elseif ($_attr['scope'] == 'global') {
  50. $_scope = Smarty::SCOPE_GLOBAL;
  51. } else {
  52. $compiler->trigger_template_error('illegal value for "scope" attribute', $compiler->lex->taglineno);
  53. }
  54. }
  55. // compiled output
  56. if (isset($parameter['smarty_internal_index'])) {
  57. $output = "<?php \$_smarty_tpl->createLocalArrayVariable($_attr[var], $_nocache, $_scope);\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value$parameter[smarty_internal_index] = $_attr[value];";
  58. } else {
  59. // implement Smarty2's behaviour of variables assigned by reference
  60. if ($compiler->template->smarty instanceof SmartyBC) {
  61. $output = "<?php if (isset(\$_smarty_tpl->tpl_vars[$_attr[var]])) {\$_smarty_tpl->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
  62. $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;";
  63. $output .= "\n} else \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_variable($_attr[value], $_nocache, $_scope);";
  64. } else {
  65. $output = "<?php \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_variable($_attr[value], $_nocache, $_scope);";
  66. }
  67. }
  68. if ($_scope == Smarty::SCOPE_PARENT) {
  69. $output .= "\nif (\$_smarty_tpl->parent != null) \$_smarty_tpl->parent->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
  70. } elseif ($_scope == Smarty::SCOPE_ROOT || $_scope == Smarty::SCOPE_GLOBAL) {
  71. $output .= "\n\$_ptr = \$_smarty_tpl->parent; while (\$_ptr != null) {\$_ptr->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]]; \$_ptr = \$_ptr->parent; }";
  72. }
  73. if ( $_scope == Smarty::SCOPE_GLOBAL) {
  74. $output .= "\nSmarty::\$global_tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
  75. }
  76. $output .= '?>';
  77. return $output;
  78. }
  79. }
  80. ?>