smarty_internal_compile_assign.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. $output = "<?php \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_variable($_attr[value], $_nocache, $_scope);";
  60. }
  61. if ($_scope == Smarty::SCOPE_PARENT) {
  62. $output .= "\nif (\$_smarty_tpl->parent != null) \$_smarty_tpl->parent->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
  63. } elseif ($_scope == Smarty::SCOPE_ROOT || $_scope == Smarty::SCOPE_GLOBAL) {
  64. $output .= "\n\$_ptr = \$_smarty_tpl->parent; while (\$_ptr != null) {\$_ptr->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]]; \$_ptr = \$_ptr->parent; }";
  65. }
  66. if ( $_scope == Smarty::SCOPE_GLOBAL) {
  67. $output .= "\nSmarty::\$global_tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
  68. }
  69. $output .= '?>';
  70. return $output;
  71. }
  72. }
  73. ?>