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 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $option_flags = array('nocache', 'noscope');
  25. /**
  26. * Valid scope names
  27. *
  28. * @var array
  29. */
  30. public $valid_scopes = array('local' => Smarty::SCOPE_LOCAL, 'parent' => Smarty::SCOPE_PARENT,
  31. 'root' => Smarty::SCOPE_ROOT, 'global' => Smarty::SCOPE_GLOBAL,
  32. 'tpl_root' => Smarty::SCOPE_TPL_ROOT, 'smarty' => Smarty::SCOPE_SMARTY);
  33. /**
  34. * Compiles code for the {assign} tag
  35. *
  36. * @param array $args array with attributes from parser
  37. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  38. * @param array $parameter array with compilation parameter
  39. *
  40. * @return string compiled code
  41. * @throws \SmartyCompilerException
  42. */
  43. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  44. {
  45. // the following must be assigned at runtime because it will be overwritten in Smarty_Internal_Compile_Append
  46. $this->required_attributes = array('var', 'value');
  47. $this->shorttag_order = array('var', 'value');
  48. $this->optional_attributes = array('scope');
  49. $this->mapCache = array();
  50. $_nocache = false;
  51. // check and get attributes
  52. $_attr = $this->getAttributes($compiler, $args);
  53. // nocache ?
  54. if ($_var = $compiler->getId($_attr[ 'var' ])) {
  55. $_var = "'{$_var}'";
  56. } else {
  57. $_var = $_attr[ 'var' ];
  58. }
  59. if ($compiler->tag_nocache || $compiler->nocache) {
  60. $_nocache = true;
  61. // create nocache var to make it know for further compiling
  62. $compiler->setNocacheInVariable($_attr[ 'var' ]);
  63. }
  64. // scope setup
  65. if ($_attr[ 'noscope' ]) {
  66. $_scope = - 1;
  67. } else {
  68. $_scope = $compiler->convertScope($_attr, $this->valid_scopes);
  69. }
  70. // optional parameter
  71. $_params = '';
  72. if ($_nocache || $_scope) {
  73. $_params .= ' ,' . var_export($_nocache, true);
  74. }
  75. if ($_scope) {
  76. $_params .= ' ,' . $_scope;
  77. }
  78. if (isset($parameter[ 'smarty_internal_index' ])) {
  79. $output =
  80. "<?php \$_tmp_array = isset(\$_smarty_tpl->tpl_vars[{$_var}]) ? \$_smarty_tpl->tpl_vars[{$_var}]->value : array();\n";
  81. $output .= "if (!is_array(\$_tmp_array) || \$_tmp_array instanceof ArrayAccess) {\n";
  82. $output .= "settype(\$_tmp_array, 'array');\n";
  83. $output .= "}\n";
  84. $output .= "\$_tmp_array{$parameter['smarty_internal_index']} = {$_attr['value']};\n";
  85. $output .= "\$_smarty_tpl->_assignInScope({$_var}, \$_tmp_array{$_params});?>";
  86. } else {
  87. $output = "<?php \$_smarty_tpl->_assignInScope({$_var}, {$_attr['value']}{$_params});?>";
  88. }
  89. return $output;
  90. }
  91. }