smarty_internal_compile_while.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 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. // check and get attributes
  30. $_attr = $this->getAttributes($compiler, $args);
  31. $this->openTag($compiler, 'while', $compiler->nocache);
  32. if (!array_key_exists("if condition", $parameter)) {
  33. $compiler->trigger_template_error("missing while condition", $compiler->lex->taglineno);
  34. }
  35. // maybe nocache because of nocache variables
  36. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  37. if (is_array($parameter['if condition'])) {
  38. if ($compiler->nocache) {
  39. $_nocache = ',true';
  40. // create nocache var to make it know for further compiling
  41. if (is_array($parameter['if condition']['var'])) {
  42. $compiler->template->tpl_vars[trim($parameter['if condition']['var']['var'], "'")] = new Smarty_variable(null, true);
  43. } else {
  44. $compiler->template->tpl_vars[trim($parameter['if condition']['var'], "'")] = new Smarty_variable(null, true);
  45. }
  46. } else {
  47. $_nocache = '';
  48. }
  49. if (is_array($parameter['if condition']['var'])) {
  50. $_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value)) \$_smarty_tpl->createLocalArrayVariable(" . $parameter['if condition']['var']['var'] . "$_nocache);\n";
  51. $_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['value'] . ") {?>";
  52. } else {
  53. $_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "])) \$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "] = new Smarty_Variable(null{$_nocache});";
  54. $_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . ") {?>";
  55. }
  56. return $_output;
  57. } else {
  58. return "<?php while ({$parameter['if condition']}) {?>";
  59. }
  60. }
  61. }
  62. /**
  63. * Smarty Internal Plugin Compile Whileclose Class
  64. *
  65. * @package Smarty
  66. * @subpackage Compiler
  67. */
  68. class Smarty_Internal_Compile_Whileclose extends Smarty_Internal_CompileBase
  69. {
  70. /**
  71. * Compiles code for the {/while} tag
  72. *
  73. * @param array $args array with attributes from parser
  74. * @param object $compiler compiler object
  75. *
  76. * @return string compiled code
  77. */
  78. public function compile($args, $compiler)
  79. {
  80. // must endblock be nocache?
  81. if ($compiler->nocache) {
  82. $compiler->tag_nocache = true;
  83. }
  84. $compiler->nocache = $this->closeTag($compiler, array('while'));
  85. return "<?php }?>";
  86. }
  87. }