smarty_internal_compile_while.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if (is_array($parameter[ 'if condition' ])) {
  40. if ($compiler->nocache) {
  41. $_nocache = ',true';
  42. // create nocache var to make it know for further compiling
  43. if (is_array($parameter[ 'if condition' ][ 'var' ])) {
  44. $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ];
  45. } else {
  46. $var = $parameter[ 'if condition' ][ 'var' ];
  47. }
  48. $compiler->setNocacheInVariable($var);
  49. } else {
  50. $_nocache = '';
  51. }
  52. $assignCompiler = new Smarty_Internal_Compile_Assign();
  53. $assignAttr = array();
  54. $assignAttr[][ 'value' ] = $parameter[ 'if condition' ][ 'value' ];
  55. if (is_array($parameter[ 'if condition' ][ 'var' ])) {
  56. $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ][ 'var' ];
  57. $_output = "<?php while (" . $parameter[ 'if condition' ][ 'value' ] . ") {?>";
  58. $_output .= $assignCompiler->compile($assignAttr, $compiler,
  59. array('smarty_internal_index' => $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ]));
  60. } else {
  61. $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ];
  62. $_output = "<?php while (" . $parameter[ 'if condition' ][ 'value' ] . ") {?>";
  63. $_output .= $assignCompiler->compile($assignAttr, $compiler, array());
  64. }
  65. return $_output;
  66. } else {
  67. return "<?php\n while ({$parameter['if condition']}) {?>";
  68. }
  69. }
  70. }
  71. /**
  72. * Smarty Internal Plugin Compile Whileclose Class
  73. *
  74. * @package Smarty
  75. * @subpackage Compiler
  76. */
  77. class Smarty_Internal_Compile_Whileclose extends Smarty_Internal_CompileBase
  78. {
  79. /**
  80. * Compiles code for the {/while} tag
  81. *
  82. * @param array $args array with attributes from parser
  83. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  84. *
  85. * @return string compiled code
  86. */
  87. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
  88. {
  89. $compiler->loopNesting --;
  90. // must endblock be nocache?
  91. if ($compiler->nocache) {
  92. $compiler->tag_nocache = true;
  93. }
  94. $compiler->nocache = $this->closeTag($compiler, array('while'));
  95. return "<?php }?>\n";
  96. }
  97. }