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_Compile_Private_ForeachSection
20 * Attribute definition: Overwrites base class.
23 * @see Smarty_Internal_CompileBase
25 public $required_attributes = array('from', 'item');
28 * Attribute definition: Overwrites base class.
31 * @see Smarty_Internal_CompileBase
33 public $optional_attributes = array('name', 'key', 'properties');
36 * Attribute definition: Overwrites base class.
39 * @see Smarty_Internal_CompileBase
41 public $shorttag_order = array('from', 'item', 'key', 'name');
55 public $tagName = 'foreach';
58 * Valid properties of $smarty.foreach.name.xxx variable
62 public $nameProperties = array('first', 'last', 'index', 'iteration', 'show', 'total');
65 * Valid properties of $item@xxx variable
69 public $itemProperties = array('first', 'last', 'index', 'iteration', 'show', 'total', 'key');
72 * Flag if tag had name attribute
76 public $isNamed = false;
79 * Compiles code for the {foreach} tag
81 * @param array $args array with attributes from parser
82 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
83 * @param array $parameter array with compilation parameter
85 * @return string compiled code
86 * @throws \SmartyCompilerException
88 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
90 $compiler->loopNesting ++;
92 $this->isNamed = false;
93 // check and get attributes
94 $_attr = $this->getAttributes($compiler, $args);
95 $from = $_attr[ 'from' ];
96 $item = $compiler->getId($_attr[ 'item' ]);
97 if ($item === false) {
98 $item = $compiler->getVariableName($_attr[ 'item' ]);
101 $attributes = array('item' => $item);
102 if (isset($_attr[ 'key' ])) {
103 $key = $compiler->getId($_attr[ 'key' ]);
104 if ($key === false) {
105 $key = $compiler->getVariableName($_attr[ 'key' ]);
107 $attributes[ 'key' ] = $key;
109 if (isset($_attr[ 'name' ])) {
110 $this->isNamed = true;
111 $name = $attributes[ 'name' ] = $compiler->getId($_attr[ 'name' ]);
113 foreach ($attributes as $a => $v) {
115 $compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", null, true);
118 $fromName = $compiler->getVariableName($_attr[ 'from' ]);
120 foreach (array('item', 'key') as $a) {
121 if (isset($attributes[ $a ]) && $attributes[ $a ] == $fromName) {
122 $compiler->trigger_template_error("'{$a}' and 'from' may not have same variable name '{$fromName}'",
128 $itemVar = "\$_smarty_tpl->tpl_vars['{$item}']";
129 $local = '$__foreach_' . $attributes[ 'item' ] . '_' . $this->counter ++ . '_';
130 // search for used tag attributes
132 $namedAttr = array();
133 $this->scanForProperties($attributes, $compiler);
134 if (!empty($this->matchResults[ 'item' ])) {
135 $itemAttr = $this->matchResults[ 'item' ];
137 if (!empty($this->matchResults[ 'named' ])) {
138 $namedAttr = $this->matchResults[ 'named' ];
140 if (isset($_attr[ 'properties' ]) && preg_match_all("/['](.*?)[']/", $_attr[ 'properties' ], $match)) {
141 foreach ($match[ 1 ] as $prop) {
142 if (in_array($prop, $this->itemProperties)) {
143 $itemAttr[ $prop ] = true;
145 $compiler->trigger_template_error("Invalid property '{$prop}'", null, true);
148 if ($this->isNamed) {
149 foreach ($match[ 1 ] as $prop) {
150 if (in_array($prop, $this->nameProperties)) {
151 $nameAttr[ $prop ] = true;
153 $compiler->trigger_template_error("Invalid property '{$prop}'", null, true);
158 if (isset($itemAttr[ 'first' ])) {
159 $itemAttr[ 'index' ] = true;
161 if (isset($namedAttr[ 'first' ])) {
162 $namedAttr[ 'index' ] = true;
164 if (isset($namedAttr[ 'last' ])) {
165 $namedAttr[ 'iteration' ] = true;
166 $namedAttr[ 'total' ] = true;
168 if (isset($itemAttr[ 'last' ])) {
169 $itemAttr[ 'iteration' ] = true;
170 $itemAttr[ 'total' ] = true;
172 if (isset($namedAttr[ 'show' ])) {
173 $namedAttr[ 'total' ] = true;
175 if (isset($itemAttr[ 'show' ])) {
176 $itemAttr[ 'total' ] = true;
179 if (isset($attributes[ 'key' ])) {
180 $keyTerm = "\$_smarty_tpl->tpl_vars['{$key}']->value => ";
182 if (isset($itemAttr[ 'key' ])) {
183 $keyTerm = "{$itemVar}->key => ";
185 if ($this->isNamed) {
186 $foreachVar = "\$_smarty_tpl->tpl_vars['__smarty_foreach_{$attributes['name']}']";
188 $needTotal = isset($itemAttr[ 'total' ]);
190 $this->openTag($compiler, 'foreach',
191 array('foreach', $compiler->nocache, $local, $itemVar, empty($itemAttr) ? 1 : 2));
192 // maybe nocache because of nocache variables
193 $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
194 // generate output code
196 $output .= "\$_from = \$_smarty_tpl->smarty->ext->_foreach->init(\$_smarty_tpl, $from, " .
197 var_export($item, true);
198 if ($name || $needTotal || $key) {
199 $output .= ', ' . var_export($needTotal, true);
202 $output .= ', ' . var_export($key, true);
205 $output .= ', ' . var_export($name, true) . ', ' . var_export($namedAttr, true);
208 if (isset($itemAttr[ 'show' ])) {
209 $output .= "{$itemVar}->show = ({$itemVar}->total > 0);\n";
211 if (isset($itemAttr[ 'iteration' ])) {
212 $output .= "{$itemVar}->iteration = 0;\n";
214 if (isset($itemAttr[ 'index' ])) {
215 $output .= "{$itemVar}->index = -1;\n";
217 $output .= "if (\$_from !== null) {\n";
218 $output .= "foreach (\$_from as {$keyTerm}{$itemVar}->value) {\n";
219 if (isset($attributes[ 'key' ]) && isset($itemAttr[ 'key' ])) {
220 $output .= "\$_smarty_tpl->tpl_vars['{$key}']->value = {$itemVar}->key;\n";
222 if (isset($itemAttr[ 'iteration' ])) {
223 $output .= "{$itemVar}->iteration++;\n";
225 if (isset($itemAttr[ 'index' ])) {
226 $output .= "{$itemVar}->index++;\n";
228 if (isset($itemAttr[ 'first' ])) {
229 $output .= "{$itemVar}->first = !{$itemVar}->index;\n";
231 if (isset($itemAttr[ 'last' ])) {
232 $output .= "{$itemVar}->last = {$itemVar}->iteration == {$itemVar}->total;\n";
234 if (isset($foreachVar)) {
235 if (isset($namedAttr[ 'iteration' ])) {
236 $output .= "{$foreachVar}->value['iteration']++;\n";
238 if (isset($namedAttr[ 'index' ])) {
239 $output .= "{$foreachVar}->value['index']++;\n";
241 if (isset($namedAttr[ 'first' ])) {
242 $output .= "{$foreachVar}->value['first'] = !{$foreachVar}->value['index'];\n";
244 if (isset($namedAttr[ 'last' ])) {
245 $output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] == {$foreachVar}->value['total'];\n";
248 if (!empty($itemAttr)) {
249 $output .= "{$local}saved = {$itemVar};\n";
257 * Compiles code for to restore saved template variables
259 * @param int $levels number of levels to restore
261 * @return string compiled code
263 public function compileRestore($levels)
265 return "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl, {$levels});\n";
270 * Smarty Internal Plugin Compile Foreachelse Class
273 * @subpackage Compiler
275 class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
278 * Compiles code for the {foreachelse} tag
280 * @param array $args array with attributes from parser
281 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
282 * @param array $parameter array with compilation parameter
284 * @return string compiled code
286 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
288 // check and get attributes
289 $_attr = $this->getAttributes($compiler, $args);
291 list($openTag, $nocache, $local, $itemVar, $restore) = $this->closeTag($compiler, array('foreach'));
292 $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $local, $itemVar, 0));
295 $output .= "{$itemVar} = {$local}saved;\n";
297 $output .= "}\n} else {\n?>\n";
303 * Smarty Internal Plugin Compile Foreachclose Class
306 * @subpackage Compiler
308 class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
311 * Compiles code for the {/foreach} tag
313 * @param array $args array with attributes from parser
314 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
315 * @param array $parameter array with compilation parameter
317 * @return string compiled code
319 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
321 $compiler->loopNesting --;
322 // must endblock be nocache?
323 if ($compiler->nocache) {
324 $compiler->tag_nocache = true;
327 list($openTag, $compiler->nocache, $local, $itemVar, $restore) =
328 $this->closeTag($compiler, array('foreach', 'foreachelse'));
332 $output .= "{$itemVar} = {$local}saved;\n";
338 /* @var Smarty_Internal_Compile_Foreach $foreachCompiler */
339 $foreachCompiler = $compiler->getTagCompiler('foreach');
340 $output .= $foreachCompiler->compileRestore(1);