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_for.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile For
  4. * Compiles the {for} {forelse} {/for} tags
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile For Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Compiles code for the {for} tag
  20. * Smarty 3 does implement two different syntax's:
  21. * - {for $var in $array}
  22. * For looping over arrays or iterators
  23. * - {for $x=0; $x<$y; $x++}
  24. * For general loops
  25. * The parser is generating different sets of attribute by which this compiler can
  26. * determine which syntax is used.
  27. *
  28. * @param array $args array with attributes from parser
  29. * @param object $compiler compiler object
  30. * @param array $parameter array with compilation parameter
  31. *
  32. * @return string compiled code
  33. */
  34. public function compile($args, $compiler, $parameter)
  35. {
  36. $compiler->loopNesting++;
  37. if ($parameter == 0) {
  38. $this->required_attributes = array('start', 'to');
  39. $this->optional_attributes = array('max', 'step');
  40. } else {
  41. $this->required_attributes = array('start', 'ifexp', 'var', 'step');
  42. $this->optional_attributes = array();
  43. }
  44. // check and get attributes
  45. $_attr = $this->getAttributes($compiler, $args);
  46. $output = "<?php\n";
  47. if ($parameter == 1) {
  48. foreach ($_attr['start'] as $_statement) {
  49. if (is_array($_statement['var'])) {
  50. $var = $_statement['var']['var'];
  51. $index = $_statement['var']['smarty_internal_index'];
  52. } else {
  53. $var = $_statement['var'];
  54. $index = '';
  55. }
  56. $output .= "\$_smarty_tpl->tpl_vars[$var] = new Smarty_Variable;\n";
  57. $output .= "\$_smarty_tpl->tpl_vars[$var]->value{$index} = {$_statement['value']};\n";
  58. }
  59. if (is_array($_attr['var'])) {
  60. $var = $_attr['var']['var'];
  61. $index = $_attr['var']['smarty_internal_index'];
  62. } else {
  63. $var = $_attr['var'];
  64. $index = '';
  65. }
  66. $output .= "if ($_attr[ifexp]) {\nfor (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$var]->value{$index}$_attr[step]) {\n";
  67. } else {
  68. $_statement = $_attr['start'];
  69. if (is_array($_statement['var'])) {
  70. $var = $_statement['var']['var'];
  71. $index = $_statement['var']['smarty_internal_index'];
  72. } else {
  73. $var = $_statement['var'];
  74. $index = '';
  75. }
  76. $output .= "\$_smarty_tpl->tpl_vars[$var] = new Smarty_Variable;";
  77. if (isset($_attr['step'])) {
  78. $output .= "\$_smarty_tpl->tpl_vars[$var]->step = $_attr[step];";
  79. } else {
  80. $output .= "\$_smarty_tpl->tpl_vars[$var]->step = 1;";
  81. }
  82. if (isset($_attr['max'])) {
  83. $output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) min(ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step)),$_attr[max]);\n";
  84. } else {
  85. $output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step));\n";
  86. }
  87. $output .= "if (\$_smarty_tpl->tpl_vars[$var]->total > 0) {\n";
  88. $output .= "for (\$_smarty_tpl->tpl_vars[$var]->value{$index} = $_statement[value], \$_smarty_tpl->tpl_vars[$var]->iteration = 1;\$_smarty_tpl->tpl_vars[$var]->iteration <= \$_smarty_tpl->tpl_vars[$var]->total;\$_smarty_tpl->tpl_vars[$var]->value{$index} += \$_smarty_tpl->tpl_vars[$var]->step, \$_smarty_tpl->tpl_vars[$var]->iteration++) {\n";
  89. $output .= "\$_smarty_tpl->tpl_vars[$var]->first = \$_smarty_tpl->tpl_vars[$var]->iteration == 1;";
  90. $output .= "\$_smarty_tpl->tpl_vars[$var]->last = \$_smarty_tpl->tpl_vars[$var]->iteration == \$_smarty_tpl->tpl_vars[$var]->total;";
  91. }
  92. $output .= "?>";
  93. $this->openTag($compiler, 'for', array('for', $compiler->nocache));
  94. // maybe nocache because of nocache variables
  95. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  96. // return compiled code
  97. return $output;
  98. }
  99. }
  100. /**
  101. * Smarty Internal Plugin Compile Forelse Class
  102. *
  103. * @package Smarty
  104. * @subpackage Compiler
  105. */
  106. class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase
  107. {
  108. /**
  109. * Compiles code for the {forelse} tag
  110. *
  111. * @param array $args array with attributes from parser
  112. * @param object $compiler compiler object
  113. * @param array $parameter array with compilation parameter
  114. *
  115. * @return string compiled code
  116. */
  117. public function compile($args, $compiler, $parameter)
  118. {
  119. // check and get attributes
  120. $_attr = $this->getAttributes($compiler, $args);
  121. list($openTag, $nocache) = $this->closeTag($compiler, array('for'));
  122. $this->openTag($compiler, 'forelse', array('forelse', $nocache));
  123. return "<?php }} else { ?>";
  124. }
  125. }
  126. /**
  127. * Smarty Internal Plugin Compile Forclose Class
  128. *
  129. * @package Smarty
  130. * @subpackage Compiler
  131. */
  132. class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase
  133. {
  134. /**
  135. * Compiles code for the {/for} tag
  136. *
  137. * @param array $args array with attributes from parser
  138. * @param object $compiler compiler object
  139. * @param array $parameter array with compilation parameter
  140. *
  141. * @return string compiled code
  142. */
  143. public function compile($args, $compiler, $parameter)
  144. {
  145. $compiler->loopNesting--;
  146. // check and get attributes
  147. $_attr = $this->getAttributes($compiler, $args);
  148. // must endblock be nocache?
  149. if ($compiler->nocache) {
  150. $compiler->tag_nocache = true;
  151. }
  152. list($openTag, $compiler->nocache) = $this->closeTag($compiler, array('for', 'forelse'));
  153. $output = "<?php }\n";
  154. if ($openTag != 'forelse') {
  155. $output .= "}\n";
  156. }
  157. $output .= "?>\n";
  158. return $output;
  159. }
  160. }