smarty_internal_compile_foreach.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Foreach
  4. * Compiles the {foreach} {foreachelse} {/foreach} tags
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Foreach Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('from', 'item');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $optional_attributes = array('name', 'key');
  32. /**
  33. * Attribute definition: Overwrites base class.
  34. *
  35. * @var array
  36. * @see Smarty_Internal_CompileBase
  37. */
  38. public $shorttag_order = array('from', 'item', 'key', 'name');
  39. /**
  40. * Compiles code for the {foreach} tag
  41. *
  42. * @param array $args array with attributes from parser
  43. * @param object $compiler compiler object
  44. * @param array $parameter array with compilation parameter
  45. *
  46. * @return string compiled code
  47. */
  48. public function compile($args, $compiler, $parameter)
  49. {
  50. // check and get attributes
  51. $_attr = $this->getAttributes($compiler, $args);
  52. $from = $_attr['from'];
  53. $item = $_attr['item'];
  54. if (!strncmp("\$_smarty_tpl->tpl_vars[$item]", $from, strlen($item) + 24)) {
  55. $compiler->trigger_template_error("item variable {$item} may not be the same variable as at 'from'", $compiler->lex->taglineno);
  56. }
  57. if (isset($_attr['key'])) {
  58. $key = $_attr['key'];
  59. } else {
  60. $key = null;
  61. }
  62. $this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $item, $key));
  63. // maybe nocache because of nocache variables
  64. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  65. if (isset($_attr['name'])) {
  66. $name = $_attr['name'];
  67. $has_name = true;
  68. $SmartyVarName = '$smarty.foreach.' . trim($name, '\'"') . '.';
  69. } else {
  70. $name = null;
  71. $has_name = false;
  72. }
  73. $ItemVarName = '$' . trim($item, '\'"') . '@';
  74. // evaluates which Smarty variables and properties have to be computed
  75. if ($has_name) {
  76. $usesSmartyFirst = strpos($compiler->lex->data, $SmartyVarName . 'first') !== false;
  77. $usesSmartyLast = strpos($compiler->lex->data, $SmartyVarName . 'last') !== false;
  78. $usesSmartyIndex = strpos($compiler->lex->data, $SmartyVarName . 'index') !== false;
  79. $usesSmartyIteration = strpos($compiler->lex->data, $SmartyVarName . 'iteration') !== false;
  80. $usesSmartyShow = strpos($compiler->lex->data, $SmartyVarName . 'show') !== false;
  81. $usesSmartyTotal = strpos($compiler->lex->data, $SmartyVarName . 'total') !== false;
  82. } else {
  83. $usesSmartyFirst = false;
  84. $usesSmartyLast = false;
  85. $usesSmartyTotal = false;
  86. $usesSmartyShow = false;
  87. }
  88. $usesPropFirst = $usesSmartyFirst || strpos($compiler->lex->data, $ItemVarName . 'first') !== false;
  89. $usesPropLast = $usesSmartyLast || strpos($compiler->lex->data, $ItemVarName . 'last') !== false;
  90. $usesPropIndex = $usesPropFirst || strpos($compiler->lex->data, $ItemVarName . 'index') !== false;
  91. $usesPropIteration = $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'iteration') !== false;
  92. $usesPropShow = strpos($compiler->lex->data, $ItemVarName . 'show') !== false;
  93. $usesPropTotal = $usesSmartyTotal || $usesSmartyShow || $usesPropShow || $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'total') !== false;
  94. // generate output code
  95. $output = "<?php ";
  96. $output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable; \$_smarty_tpl->tpl_vars[$item]->_loop = false;\n";
  97. if ($key != null) {
  98. $output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
  99. }
  100. $output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
  101. if ($usesPropTotal) {
  102. $output .= " \$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n";
  103. }
  104. if ($usesPropIteration) {
  105. $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
  106. }
  107. if ($usesPropIndex) {
  108. $output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
  109. }
  110. if ($usesPropShow) {
  111. $output .= " \$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
  112. }
  113. if ($has_name) {
  114. if ($usesSmartyTotal) {
  115. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
  116. }
  117. if ($usesSmartyIteration) {
  118. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n";
  119. }
  120. if ($usesSmartyIndex) {
  121. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n";
  122. }
  123. if ($usesSmartyShow) {
  124. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['show']=(\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
  125. }
  126. }
  127. $output .= "foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value) {\n\$_smarty_tpl->tpl_vars[$item]->_loop = true;\n";
  128. if ($key != null) {
  129. $output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
  130. }
  131. if ($usesPropIteration) {
  132. $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
  133. }
  134. if ($usesPropIndex) {
  135. $output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
  136. }
  137. if ($usesPropFirst) {
  138. $output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n";
  139. }
  140. if ($usesPropLast) {
  141. $output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
  142. }
  143. if ($has_name) {
  144. if ($usesSmartyFirst) {
  145. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
  146. }
  147. if ($usesSmartyIteration) {
  148. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
  149. }
  150. if ($usesSmartyIndex) {
  151. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
  152. }
  153. if ($usesSmartyLast) {
  154. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
  155. }
  156. }
  157. $output .= "?>";
  158. return $output;
  159. }
  160. }
  161. /**
  162. * Smarty Internal Plugin Compile Foreachelse Class
  163. *
  164. * @package Smarty
  165. * @subpackage Compiler
  166. */
  167. class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
  168. {
  169. /**
  170. * Compiles code for the {foreachelse} tag
  171. *
  172. * @param array $args array with attributes from parser
  173. * @param object $compiler compiler object
  174. * @param array $parameter array with compilation parameter
  175. *
  176. * @return string compiled code
  177. */
  178. public function compile($args, $compiler, $parameter)
  179. {
  180. // check and get attributes
  181. $_attr = $this->getAttributes($compiler, $args);
  182. list($openTag, $nocache, $item, $key) = $this->closeTag($compiler, array('foreach'));
  183. $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key));
  184. return "<?php }\nif (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
  185. }
  186. }
  187. /**
  188. * Smarty Internal Plugin Compile Foreachclose Class
  189. *
  190. * @package Smarty
  191. * @subpackage Compiler
  192. */
  193. class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
  194. {
  195. /**
  196. * Compiles code for the {/foreach} tag
  197. *
  198. * @param array $args array with attributes from parser
  199. * @param object $compiler compiler object
  200. * @param array $parameter array with compilation parameter
  201. *
  202. * @return string compiled code
  203. */
  204. public function compile($args, $compiler, $parameter)
  205. {
  206. // check and get attributes
  207. $_attr = $this->getAttributes($compiler, $args);
  208. // must endblock be nocache?
  209. if ($compiler->nocache) {
  210. $compiler->tag_nocache = true;
  211. }
  212. list($openTag, $compiler->nocache, $item, $key) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
  213. return "<?php } ?>";
  214. }
  215. }