smarty_internal_compile_while.php 3.6 KB

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