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

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