3 * Smarty Internal Plugin Compile Foreach
4 * Compiles the {foreach} {foreachelse} {/foreach} tags
12 * Smarty Internal Plugin Compile Foreach Class
15 * @subpackage Compiler
17 class Smarty_Internal_Compile_Foreach
extends Smarty_Internal_CompileBase
20 * Attribute definition: Overwrites base class.
23 * @see Smarty_Internal_CompileBase
25 public $required_attributes = array('from', 'item');
27 * Attribute definition: Overwrites base class.
30 * @see Smarty_Internal_CompileBase
32 public $optional_attributes = array('name', 'key');
34 * Attribute definition: Overwrites base class.
37 * @see Smarty_Internal_CompileBase
39 public $shorttag_order = array('from', 'item', 'key', 'name');
42 * Compiles code for the {foreach} tag
44 * @param array $args array with attributes from parser
45 * @param object $compiler compiler object
46 * @param array $parameter array with compilation parameter
48 * @return string compiled code
50 public function compile($args, $compiler, $parameter)
52 // check and get attributes
53 $_attr = $this->getAttributes($compiler, $args);
55 $from = $_attr['from'];
56 $item = $_attr['item'];
57 if (!strncmp("\$_smarty_tpl->tpl_vars[$item]", $from, strlen($item) +
24)) {
58 $compiler->trigger_template_error("item variable {$item} may not be the same variable as at 'from'", $compiler->lex
->taglineno
);
61 if (isset($_attr['key'])) {
67 $this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache
, $item, $key, true));
68 // maybe nocache because of nocache variables
69 $compiler->nocache
= $compiler->nocache |
$compiler->tag_nocache
;
71 if (isset($_attr['name'])) {
72 $name = trim($_attr['name'], '\'"');
74 $SmartyVarName = "\$smarty.foreach.{$name}.";
78 $ItemVarName = '$' . trim($item, '\'"') . '@';
79 // evaluates which Smarty variables and properties have to be computed
82 $useSmartyForeach = $usesSmartyFirst = strpos($compiler->lex
->data
, $SmartyVarName . 'first') !== false;
83 $useSmartyForeach = ($usesSmartyLast = strpos($compiler->lex
->data
, $SmartyVarName . 'last') !== false) ||
$useSmartyForeach;
84 $useSmartyForeach = ($usesSmartyIndex = strpos($compiler->lex
->data
, $SmartyVarName . 'index') !== false) ||
$useSmartyForeach;
85 $useSmartyForeach = ($usesSmartyIteration = (!$usesSmartyIndex && ($usesSmartyFirst ||
$usesSmartyLast)) ||
strpos($compiler->lex
->data
, $SmartyVarName . 'iteration') !== false) ||
$useSmartyForeach;
86 $useSmartyForeach = ($usesSmartyShow = strpos($compiler->lex
->data
, $SmartyVarName . 'show') !== false) ||
$useSmartyForeach;
87 $useSmartyForeach = ($usesSmartyTotal = $usesSmartyLast ||
strpos($compiler->lex
->data
, $SmartyVarName . 'total') !== false) ||
$useSmartyForeach;
89 $usesSmartyFirst = false;
90 $usesSmartyLast = false;
91 $usesSmartyTotal = false;
92 $usesSmartyShow = false;
93 $useSmartyForeach = false;
96 $usesPropKey = strpos($compiler->lex
->data
, $ItemVarName . 'key') !== false;
97 $usesPropFirst = strpos($compiler->lex
->data
, $ItemVarName . 'first') !== false;
98 $usesPropLast = strpos($compiler->lex
->data
, $ItemVarName . 'last') !== false;
99 $usesPropIndex = strpos($compiler->lex
->data
, $ItemVarName . 'index') !== false;
100 $usesPropIteration = (!$usesPropIndex && ($usesPropFirst ||
$usesPropLast)) ||
strpos($compiler->lex
->data
, $ItemVarName . 'iteration') !== false;
101 $usesPropShow = strpos($compiler->lex
->data
, $ItemVarName . 'show') !== false;
102 $usesPropTotal = $usesPropLast ||
strpos($compiler->lex
->data
, $ItemVarName . 'total') !== false;
106 $keyTerm = "\$_smarty_tpl->tpl_vars[$item]->key => ";
107 } elseif ($key != null) {
108 $keyTerm = "\$_smarty_tpl->tpl_vars[$key]->value => ";
110 // generate output code
112 $output .= "\$_from = $from;\n";
113 $output .= "if (!is_array(\$_from) && !is_object(\$_from)) {\n";
114 $output .= "settype(\$_from, 'array');\n";
116 $output .= "\$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
117 $output .= "\$_smarty_tpl->tpl_vars[$item]->_loop = false;\n";
119 $output .= "\$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
121 if ($usesPropTotal) {
122 $output .= "\$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n";
124 if ($usesPropIteration) {
125 $output .= "\$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
127 if ($usesPropIndex) {
128 $output .= "\$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
131 if ($usesPropTotal) {
132 $output .= "\$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
134 $output .= "\$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->_count(\$_from) > 0);\n";
139 if ($usesSmartyTotal) {
140 $prop['total'] = "'total' => ";
141 $prop['total'] .= $usesSmartyShow ?
'$total = ' : '';
142 $prop['total'] .= '$_smarty_tpl->_count($_from)';
144 if ($usesSmartyIteration) {
145 $prop['iteration'] = "'iteration' => 0";
147 if ($usesSmartyIndex) {
148 $prop['index'] = "'index' => -1";
150 if ($usesSmartyShow) {
151 $prop['show'] = "'show' => ";
152 if ($usesSmartyTotal) {
153 $prop['show'] .= "(\$total > 0)";
155 $prop['show'] .= "(\$_smarty_tpl->_count(\$_from) > 0)";
158 if ($useSmartyForeach) {
159 $_vars = 'array(' . join(', ', $prop) . ')';
160 $foreachVar = "'__foreach_{$name}'";
161 $output .= "\$_smarty_tpl->tpl_vars[$foreachVar] = new Smarty_Variable({$_vars});\n";
164 $output .= "foreach (\$_from as {$keyTerm}\$_smarty_tpl->tpl_vars[$item]->value) {\n";
165 $output .= "\$_smarty_tpl->tpl_vars[$item]->_loop = true;\n";
166 if ($key != null && $usesPropKey) {
167 $output .= "\$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
169 if ($usesPropIteration) {
170 $output .= "\$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
172 if ($usesPropIndex) {
173 $output .= "\$_smarty_tpl->tpl_vars[$item]->index++;\n";
175 if ($usesPropFirst) {
176 if ($usesPropIndex) {
177 $output .= "\$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index == 0;\n";
179 $output .= "\$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->iteration == 1;\n";
183 if ($usesPropIndex) {
184 $output .= "\$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->index + 1 == \$_smarty_tpl->tpl_vars[$item]->total;\n";
186 $output .= "\$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration == \$_smarty_tpl->tpl_vars[$item]->total;\n";
190 if ($usesSmartyIteration) {
191 $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration']++;\n";
193 if ($usesSmartyIndex) {
194 $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['index']++;\n";
196 if ($usesSmartyFirst) {
197 if ($usesSmartyIndex) {
198 $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['first'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['index'] == 0;\n";
200 $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['first'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration'] == 1;\n";
203 if ($usesSmartyLast) {
204 if ($usesSmartyIndex) {
205 $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['last'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['index'] + 1 == \$_smarty_tpl->tpl_vars[$foreachVar]->value['total'];\n";
207 $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['last'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration'] == \$_smarty_tpl->tpl_vars[$foreachVar]->value['total'];\n";
211 $itemName = trim($item,"'\"");
212 $output .= "\$foreach_{$itemName}_Sav = \$_smarty_tpl->tpl_vars[$item];\n";
220 * Smarty Internal Plugin Compile Foreachelse Class
223 * @subpackage Compiler
225 class Smarty_Internal_Compile_Foreachelse
extends Smarty_Internal_CompileBase
228 * Compiles code for the {foreachelse} tag
230 * @param array $args array with attributes from parser
231 * @param object $compiler compiler object
232 * @param array $parameter array with compilation parameter
234 * @return string compiled code
236 public function compile($args, $compiler, $parameter)
238 // check and get attributes
239 $_attr = $this->getAttributes($compiler, $args);
241 list($openTag, $nocache, $item, $key, $foo) = $this->closeTag($compiler, array('foreach'));
242 $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key, false));
243 $itemName = trim($item,"'\"");
245 $output .= "\$_smarty_tpl->tpl_vars[$item] = \$foreach_{$itemName}_Sav;\n";
247 $output .= "if (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
253 * Smarty Internal Plugin Compile Foreachclose Class
256 * @subpackage Compiler
258 class Smarty_Internal_Compile_Foreachclose
extends Smarty_Internal_CompileBase
261 * Compiles code for the {/foreach} tag
263 * @param array $args array with attributes from parser
264 * @param object $compiler compiler object
265 * @param array $parameter array with compilation parameter
267 * @return string compiled code
269 public function compile($args, $compiler, $parameter)
271 // check and get attributes
272 $_attr = $this->getAttributes($compiler, $args);
273 // must endblock be nocache?
274 if ($compiler->nocache
) {
275 $compiler->tag_nocache
= true;
278 list($openTag, $compiler->nocache
, $item, $key, $restore) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
279 $itemName = trim($item,"'\"");
282 $output .= "\$_smarty_tpl->tpl_vars[$item] = \$foreach_{$itemName}_Sav;\n";