smarty_internal_compile_foreach.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
  15. /**
  16. * Compiles code for the {foreach} tag
  17. *
  18. * @param array $args array with attributes from parser
  19. * @param object $compiler compiler object
  20. * @return string compiled code
  21. */
  22. public function compile($args, $compiler)
  23. {
  24. $this->compiler = $compiler;
  25. $this->required_attributes = array('from', 'item');
  26. $this->optional_attributes = array('name', 'key');
  27. $tpl = $compiler->template;
  28. // check and get attributes
  29. $_attr = $this->_get_attributes($args);
  30. $from = $_attr['from'];
  31. $item = $_attr['item'];
  32. if (substr_compare("\$_smarty_tpl->getVariable($item)", $from,0, strlen("\$_smarty_tpl->getVariable($item)")) == 0) {
  33. $this->compiler->trigger_template_error("item parameter {$item} may not be the same parameter at 'from'");
  34. }
  35. if (isset($_attr['key'])) {
  36. $key = $_attr['key'];
  37. } else {
  38. $key = null;
  39. }
  40. $this->_open_tag('foreach', array('foreach', $this->compiler->nocache, $item, $key));
  41. // maybe nocache because of nocache variables
  42. $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
  43. if (isset($_attr['name'])) {
  44. $name = $_attr['name'];
  45. $has_name = true;
  46. $SmartyVarName = '$smarty.foreach.' . trim($name, '\'"') . '.';
  47. } else {
  48. $name = null;
  49. $has_name = false;
  50. }
  51. $ItemVarName = '$' . trim($item, '\'"') . '@';
  52. // evaluates which Smarty variables and properties have to be computed
  53. if ($has_name) {
  54. $usesSmartyFirst = strpos($tpl->template_source, $SmartyVarName . 'first') !== false;
  55. $usesSmartyLast = strpos($tpl->template_source, $SmartyVarName . 'last') !== false;
  56. $usesSmartyIndex = strpos($tpl->template_source, $SmartyVarName . 'index') !== false;
  57. $usesSmartyIteration = strpos($tpl->template_source, $SmartyVarName . 'iteration') !== false;
  58. $usesSmartyShow = strpos($tpl->template_source, $SmartyVarName . 'show') !== false;
  59. $usesSmartyTotal = $usesSmartyLast || strpos($tpl->template_source, $SmartyVarName . 'total') !== false;
  60. } else {
  61. $usesSmartyFirst = false;
  62. $usesSmartyLast = false;
  63. $usesSmartyTotal = false;
  64. }
  65. $usesPropFirst = $usesSmartyFirst || strpos($tpl->template_source, $ItemVarName . 'first') !== false;
  66. $usesPropLast = $usesSmartyLast || strpos($tpl->template_source, $ItemVarName . 'last') !== false;
  67. $usesPropIndex = $usesPropFirst || strpos($tpl->template_source, $ItemVarName . 'index') !== false;
  68. $usesPropIteration = $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'iteration') !== false;
  69. $usesPropShow = strpos($tpl->template_source, $ItemVarName . 'show') !== false;
  70. $usesPropTotal = $usesSmartyTotal || $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'total') !== false;
  71. // generate output code
  72. $output = "<?php ";
  73. $output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
  74. $compiler->local_var[$item] = true;
  75. if ($key != null) {
  76. $output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
  77. $compiler->local_var[$key] = true;
  78. }
  79. $output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
  80. if ($usesPropTotal) {
  81. $output .= " \$_smarty_tpl->tpl_vars[$item]->total=(\$_from instanceof Traversable)?iterator_count(\$_from):count(\$_from);\n";
  82. }
  83. if ($usesPropIteration) {
  84. $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
  85. }
  86. if ($usesPropIndex) {
  87. $output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
  88. }
  89. if ($has_name) {
  90. if ($usesSmartyTotal) {
  91. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
  92. }
  93. if ($usesSmartyIteration) {
  94. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n";
  95. }
  96. if ($usesSmartyIndex) {
  97. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n";
  98. }
  99. }
  100. $output .= "if (count(\$_from) > 0){\n";
  101. $output .= " foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value){\n";
  102. if ($key != null) {
  103. $output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
  104. }
  105. if ($usesPropIteration) {
  106. $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
  107. }
  108. if ($usesPropIndex) {
  109. $output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
  110. }
  111. if ($usesPropFirst) {
  112. $output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n";
  113. }
  114. if ($usesPropLast) {
  115. $output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
  116. }
  117. if ($has_name) {
  118. if ($usesSmartyFirst) {
  119. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
  120. }
  121. if ($usesSmartyIteration) {
  122. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
  123. }
  124. if ($usesSmartyIndex) {
  125. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
  126. }
  127. if ($usesSmartyLast) {
  128. $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
  129. }
  130. }
  131. $output .= "?>";
  132. return $output;
  133. }
  134. }
  135. /**
  136. * Smarty Internal Plugin Compile Foreachelse Class
  137. */
  138. class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase {
  139. /**
  140. * Compiles code for the {foreachelse} tag
  141. *
  142. * @param array $args array with attributes from parser
  143. * @param object $compiler compiler object
  144. * @return string compiled code
  145. */
  146. public function compile($args, $compiler)
  147. {
  148. $this->compiler = $compiler;
  149. // check and get attributes
  150. $_attr = $this->_get_attributes($args);
  151. list($_open_tag, $nocache, $item, $key) = $this->_close_tag(array('foreach'));
  152. $this->_open_tag('foreachelse', array('foreachelse', $nocache, $item, $key));
  153. return "<?php }} else { ?>";
  154. }
  155. }
  156. /**
  157. * Smarty Internal Plugin Compile Foreachclose Class
  158. */
  159. class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase {
  160. /**
  161. * Compiles code for the {/foreach} tag
  162. *
  163. * @param array $args array with attributes from parser
  164. * @param object $compiler compiler object
  165. * @return string compiled code
  166. */
  167. public function compile($args, $compiler)
  168. {
  169. $this->compiler = $compiler;
  170. // check and get attributes
  171. $_attr = $this->_get_attributes($args);
  172. // must endblock be nocache?
  173. if ($this->compiler->nocache) {
  174. $this->compiler->tag_nocache = true;
  175. }
  176. list($_open_tag, $this->compiler->nocache, $item, $key) = $this->_close_tag(array('foreach', 'foreachelse'));
  177. unset($compiler->local_var[$item]);
  178. if ($key != null) {
  179. unset($compiler->local_var[$key]);
  180. }
  181. if ($_open_tag == 'foreachelse')
  182. return "<?php } ?>";
  183. else
  184. return "<?php }} ?>";
  185. }
  186. }
  187. ?>