smarty_internal_compile_foreach.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Foreach
  4. *
  5. * Compiles the {foreach} {foreachelse} {/foreach} tags
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Foreach Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
  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. * @return string compiled code
  46. */
  47. public function compile($args, $compiler, $parameter)
  48. {
  49. $tpl = $compiler->template;
  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($tpl->source->content, $SmartyVarName . 'first') !== false;
  77. $usesSmartyLast = strpos($tpl->source->content, $SmartyVarName . 'last') !== false;
  78. $usesSmartyIndex = strpos($tpl->source->content, $SmartyVarName . 'index') !== false;
  79. $usesSmartyIteration = strpos($tpl->source->content, $SmartyVarName . 'iteration') !== false;
  80. $usesSmartyShow = strpos($tpl->source->content, $SmartyVarName . 'show') !== false;
  81. $usesSmartyTotal = strpos($tpl->source->content, $SmartyVarName . 'total') !== false;
  82. } else {
  83. $usesSmartyFirst = false;
  84. $usesSmartyLast = false;
  85. $usesSmartyTotal = false;
  86. $usesSmartyShow = false;
  87. }
  88. $usesPropFirst = $usesSmartyFirst || strpos($tpl->source->content, $ItemVarName . 'first') !== false;
  89. $usesPropLast = $usesSmartyLast || strpos($tpl->source->content, $ItemVarName . 'last') !== false;
  90. $usesPropIndex = $usesPropFirst || strpos($tpl->source->content, $ItemVarName . 'index') !== false;
  91. $usesPropIteration = $usesPropLast || strpos($tpl->source->content, $ItemVarName . 'iteration') !== false;
  92. $usesPropShow = strpos($tpl->source->content, $ItemVarName . 'show') !== false;
  93. $usesPropTotal = $usesSmartyTotal || $usesSmartyShow || $usesPropShow || $usesPropLast || strpos($tpl->source->content, $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. * Compiles code for the {foreachelse} tag
  170. *
  171. * @param array $args array with attributes from parser
  172. * @param object $compiler compiler object
  173. * @param array $parameter array with compilation parameter
  174. * @return string compiled code
  175. */
  176. public function compile($args, $compiler, $parameter)
  177. {
  178. // check and get attributes
  179. $_attr = $this->getAttributes($compiler, $args);
  180. list($openTag, $nocache, $item, $key) = $this->closeTag($compiler, array('foreach'));
  181. $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key));
  182. return "<?php }\nif (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
  183. }
  184. }
  185. /**
  186. * Smarty Internal Plugin Compile Foreachclose Class
  187. *
  188. * @package Smarty
  189. * @subpackage Compiler
  190. */
  191. class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase {
  192. /**
  193. * Compiles code for the {/foreach} tag
  194. *
  195. * @param array $args array with attributes from parser
  196. * @param object $compiler compiler object
  197. * @param array $parameter array with compilation parameter
  198. * @return string compiled code
  199. */
  200. public function compile($args, $compiler, $parameter)
  201. {
  202. // check and get attributes
  203. $_attr = $this->getAttributes($compiler, $args);
  204. // must endblock be nocache?
  205. if ($compiler->nocache) {
  206. $compiler->tag_nocache = true;
  207. }
  208. list($openTag, $compiler->nocache, $item, $key) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
  209. return "<?php } ?>";
  210. }
  211. }
  212. ?>