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_while.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile While
  4. * Compiles the {while} tag
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile While Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Compiles code for the {while} tag
  20. *
  21. * @param array $args array with attributes from parser
  22. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  23. * @param array $parameter array with compilation parameter
  24. *
  25. * @return string compiled code
  26. * @throws \SmartyCompilerException
  27. */
  28. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  29. {
  30. $compiler->loopNesting++;
  31. // check and get attributes
  32. $_attr = $this->getAttributes($compiler, $args);
  33. $this->openTag($compiler, 'while', $compiler->nocache);
  34. if (!array_key_exists("if condition", $parameter)) {
  35. $compiler->trigger_template_error("missing while condition", null, true);
  36. }
  37. // maybe nocache because of nocache variables
  38. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  39. $_output = "<?php\n";
  40. if (is_array($parameter['if condition'])) {
  41. if ($compiler->nocache) {
  42. $_nocache = ',true';
  43. // create nocache var to make it know for further compiling
  44. if (is_array($parameter['if condition']['var'])) {
  45. $var = trim($parameter['if condition']['var']['var'], "'");
  46. } else {
  47. $var = trim($parameter['if condition']['var'], "'");
  48. }
  49. if (isset($compiler->template->tpl_vars[$var])) {
  50. $compiler->template->tpl_vars[$var]->nocache = true;
  51. } else {
  52. $compiler->template->tpl_vars[$var] = new Smarty_Variable(null, true);
  53. }
  54. } else {
  55. $_nocache = '';
  56. }
  57. if (is_array($parameter['if condition']['var'])) {
  58. $_output .= "if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
  59. "]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
  60. "]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " . $parameter['if condition']['var']['var'] .
  61. "$_nocache);\n";
  62. $_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
  63. $parameter['if condition']['var']['smarty_internal_index'] . " = " .
  64. $parameter['if condition']['value'] . ") {?>";
  65. } else {
  66. $_output .= "if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] .
  67. "])) \$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] .
  68. "] = new Smarty_Variable(null{$_nocache});";
  69. $_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " .
  70. $parameter['if condition']['value'] . ") {?>";
  71. }
  72. } else {
  73. $_output .= "while ({$parameter['if condition']}) {?>";
  74. }
  75. return $_output;
  76. }
  77. }
  78. /**
  79. * Smarty Internal Plugin Compile Whileclose Class
  80. *
  81. * @package Smarty
  82. * @subpackage Compiler
  83. */
  84. class Smarty_Internal_Compile_Whileclose extends Smarty_Internal_CompileBase
  85. {
  86. /**
  87. * Compiles code for the {/while} tag
  88. *
  89. * @param array $args array with attributes from parser
  90. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  91. *
  92. * @return string compiled code
  93. */
  94. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
  95. {
  96. $compiler->loopNesting--;
  97. // must endblock be nocache?
  98. if ($compiler->nocache) {
  99. $compiler->tag_nocache = true;
  100. }
  101. $compiler->nocache = $this->closeTag($compiler, array('while'));
  102. return "<?php }?>\n";
  103. }
  104. }