3 * Smarty Internal Plugin Compile Section
4 * Compiles the {section} {sectionelse} {/section} tags
12 * Smarty Internal Plugin Compile Section Class
15 * @subpackage Compiler
17 class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_ForeachSection
20 * Attribute definition: Overwrites base class.
23 * @see Smarty_Internal_CompileBase
25 public $required_attributes = array('name', 'loop');
28 * Attribute definition: Overwrites base class.
31 * @see Smarty_Internal_CompileBase
33 public $shorttag_order = array('name', 'loop');
36 * Attribute definition: Overwrites base class.
39 * @see Smarty_Internal_CompileBase
41 public $optional_attributes = array('start', 'step', 'max', 'show', 'properties');
55 public $tagName = 'section';
58 * Valid properties of $smarty.section.name.xxx variable
62 public $nameProperties = array('first', 'last', 'index', 'iteration', 'show', 'total', 'rownum', 'index_prev',
63 'index_next', 'loop');
66 * {section} tag has no item properties
70 public $itemProperties = null;
73 * {section} tag has always name attribute
77 public $isNamed = true;
80 * Compiles code for the {section} tag
82 * @param array $args array with attributes from parser
83 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
85 * @return string compiled code
86 * @throws \SmartyCompilerException
88 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
90 $compiler->loopNesting ++;
91 // check and get attributes
92 $_attr = $this->getAttributes($compiler, $args);
93 $attributes = array('name' => $compiler->getId($_attr[ 'name' ]));
94 unset($_attr[ 'name' ]);
95 foreach ($attributes as $a => $v) {
97 $compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", null, true);
100 $local = "\$__section_{$attributes['name']}_" . $this->counter ++ . '_';
101 $sectionVar = "\$_smarty_tpl->tpl_vars['__smarty_section_{$attributes['name']}']";
102 $this->openTag($compiler, 'section', array('section', $compiler->nocache, $local, $sectionVar));
103 // maybe nocache because of nocache variables
104 $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
107 array('saved' => "isset(\$_smarty_tpl->tpl_vars['__smarty_section_{$attributes['name']}']) ? \$_smarty_tpl->tpl_vars['__smarty_section_{$attributes['name']}'] : false",);
108 $initNamedProperty = array();
112 $propValue = array('index' => "{$sectionVar}->value['index']", 'show' => 'true', 'step' => 1,
113 'iteration' => "{$local}iteration",
116 $propType = array('index' => 2, 'iteration' => 2, 'show' => 0, 'step' => 0,);
117 // search for used tag attributes
118 $this->scanForProperties($attributes, $compiler);
119 if (!empty($this->matchResults[ 'named' ])) {
120 $namedAttr = $this->matchResults[ 'named' ];
122 if (isset($_attr[ 'properties' ]) && preg_match_all("/['](.*?)[']/", $_attr[ 'properties' ], $match)) {
123 foreach ($match[ 1 ] as $prop) {
124 if (in_array($prop, $this->nameProperties)) {
125 $namedAttr[ $prop ] = true;
127 $compiler->trigger_template_error("Invalid property '{$prop}'", null, true);
131 $namedAttr[ 'index' ] = true;
133 foreach ($_attr as $attr_name => $attr_value) {
134 switch ($attr_name) {
136 if (is_numeric($attr_value)) {
137 $v = (int) $attr_value;
140 $v = "(is_array(@\$_loop=$attr_value) ? count(\$_loop) : max(0, (int) \$_loop))";
143 if (isset($namedAttr[ 'loop' ])) {
144 $initNamedProperty[ 'loop' ] = "'loop' => {$v}";
146 $v = "{$sectionVar}->value['loop']";
149 $initLocal[ 'loop' ] = $v;
154 if (is_bool($attr_value)) {
155 $v = $attr_value ? 'true' : 'false';
158 $v = "(bool) $attr_value";
163 if (is_numeric($attr_value)) {
164 $v = (int) $attr_value;
165 $v = ($v == 0) ? 1 : $v;
169 $initLocal[ 'step' ] = "((int)@$attr_value) == 0 ? 1 : (int)@$attr_value";
176 if (is_numeric($attr_value)) {
177 $v = (int) $attr_value;
181 $v = "(int)@$attr_value";
185 if ($t == 3 && $compiler->getId($attr_value)) {
188 $propValue[ $attr_name ] = $v;
189 $propType[ $attr_name ] = $t;
192 if (isset($namedAttr[ 'step' ])) {
193 $initNamedProperty[ 'step' ] = $propValue[ 'step' ];
195 if (isset($namedAttr[ 'iteration' ])) {
196 $propValue[ 'iteration' ] = "{$sectionVar}->value['iteration']";
198 $incFor[ 'iteration' ] = "{$propValue['iteration']}++";
199 $initFor[ 'iteration' ] = "{$propValue['iteration']} = 1";
201 if ($propType[ 'step' ] == 0) {
202 if ($propValue[ 'step' ] == 1) {
203 $incFor[ 'index' ] = "{$sectionVar}->value['index']++";
204 } elseif ($propValue[ 'step' ] > 1) {
205 $incFor[ 'index' ] = "{$sectionVar}->value['index'] += {$propValue['step']}";
207 $incFor[ 'index' ] = "{$sectionVar}->value['index'] -= " . - $propValue[ 'step' ];
210 $incFor[ 'index' ] = "{$sectionVar}->value['index'] += {$propValue['step']}";
213 if (!isset($propValue[ 'max' ])) {
214 $propValue[ 'max' ] = $propValue[ 'loop' ];
215 $propType[ 'max' ] = $propType[ 'loop' ];
216 } elseif ($propType[ 'max' ] != 0) {
217 $propValue[ 'max' ] = "{$propValue['max']} < 0 ? {$propValue['loop']} : {$propValue['max']}";
218 $propType[ 'max' ] = 1;
220 if ($propValue[ 'max' ] < 0) {
221 $propValue[ 'max' ] = $propValue[ 'loop' ];
222 $propType[ 'max' ] = $propType[ 'loop' ];
226 if (!isset($propValue[ 'start' ])) {
228 array(1 => "{$propValue['step']} > 0 ? ", 2 => '0', 3 => ' : ', 4 => $propValue[ 'loop' ], 5 => ' - 1');
229 if ($propType[ 'loop' ] == 0) {
230 $start_code[ 5 ] = '';
231 $start_code[ 4 ] = $propValue[ 'loop' ] - 1;
233 if ($propType[ 'step' ] == 0) {
234 if ($propValue[ 'step' ] > 0) {
235 $start_code = array(1 => '0');
236 $propType[ 'start' ] = 0;
238 $start_code[ 1 ] = $start_code[ 2 ] = $start_code[ 3 ] = '';
239 $propType[ 'start' ] = $propType[ 'loop' ];
242 $propType[ 'start' ] = 1;
244 $propValue[ 'start' ] = join('', $start_code);
247 array(1 => "{$propValue['start']} < 0 ? ", 2 => 'max(', 3 => "{$propValue['step']} > 0 ? ", 4 => '0',
248 5 => ' : ', 6 => '-1', 7 => ', ', 8 => "{$propValue['start']} + {$propValue['loop']}", 10 => ')',
249 11 => ' : ', 12 => 'min(', 13 => $propValue[ 'start' ], 14 => ', ',
250 15 => "{$propValue['step']} > 0 ? ", 16 => $propValue[ 'loop' ], 17 => ' : ',
251 18 => $propType[ 'loop' ] == 0 ? $propValue[ 'loop' ] - 1 : "{$propValue['loop']} - 1",
253 if ($propType[ 'step' ] == 0) {
254 $start_code[ 3 ] = $start_code[ 5 ] = $start_code[ 15 ] = $start_code[ 17 ] = '';
255 if ($propValue[ 'step' ] > 0) {
256 $start_code[ 6 ] = $start_code[ 18 ] = '';
258 $start_code[ 4 ] = $start_code[ 16 ] = '';
261 if ($propType[ 'start' ] == 0) {
262 if ($propType[ 'loop' ] == 0) {
263 $start_code[ 8 ] = $propValue[ 'start' ] + $propValue[ 'loop' ];
265 $propType[ 'start' ] = $propType[ 'step' ] + $propType[ 'loop' ];
266 $start_code[ 1 ] = '';
267 if ($propValue[ 'start' ] < 0) {
268 for ($i = 11; $i <= 19; $i ++) {
269 $start_code[ $i ] = '';
271 if ($propType[ 'start' ] == 0) {
272 $start_code = array(max($propValue[ 'step' ] > 0 ? 0 : - 1,
273 $propValue[ 'start' ] + $propValue[ 'loop' ]));
276 for ($i = 1; $i <= 11; $i ++) {
277 $start_code[ $i ] = '';
279 if ($propType[ 'start' ] == 0) {
281 array(min($propValue[ 'step' ] > 0 ? $propValue[ 'loop' ] : $propValue[ 'loop' ] - 1,
282 $propValue[ 'start' ]));
286 $propValue[ 'start' ] = join('', $start_code);
288 if ($propType[ 'start' ] != 0) {
289 $initLocal[ 'start' ] = $propValue[ 'start' ];
290 $propValue[ 'start' ] = "{$local}start";
293 $initFor[ 'index' ] = "{$sectionVar}->value['index'] = {$propValue['start']}";
295 if (!isset($_attr[ 'start' ]) && !isset($_attr[ 'step' ]) && !isset($_attr[ 'max' ])) {
296 $propValue[ 'total' ] = $propValue[ 'loop' ];
297 $propType[ 'total' ] = $propType[ 'loop' ];
299 $propType[ 'total' ] =
300 $propType[ 'start' ] + $propType[ 'loop' ] + $propType[ 'step' ] + $propType[ 'max' ];
301 if ($propType[ 'total' ] == 0) {
302 $propValue[ 'total' ] =
303 min(ceil(($propValue[ 'step' ] > 0 ? $propValue[ 'loop' ] - $propValue[ 'start' ] :
304 (int) $propValue[ 'start' ] + 1) / abs($propValue[ 'step' ])), $propValue[ 'max' ]);
306 $total_code = array(1 => 'min(', 2 => 'ceil(', 3 => '(', 4 => "{$propValue['step']} > 0 ? ",
307 5 => $propValue[ 'loop' ], 6 => ' - ', 7 => $propValue[ 'start' ], 8 => ' : ',
308 9 => $propValue[ 'start' ], 10 => '+ 1', 11 => ')', 12 => '/ ', 13 => 'abs(',
309 14 => $propValue[ 'step' ], 15 => ')', 16 => ')', 17 => ", {$propValue['max']})",);
310 if (!isset($propValue[ 'max' ])) {
311 $total_code[ 1 ] = $total_code[ 17 ] = '';
313 if ($propType[ 'loop' ] + $propType[ 'start' ] == 0) {
314 $total_code[ 5 ] = $propValue[ 'loop' ] - $propValue[ 'start' ];
315 $total_code[ 6 ] = $total_code[ 7 ] = '';
317 if ($propType[ 'start' ] == 0) {
318 $total_code[ 9 ] = (int) $propValue[ 'start' ] + 1;
319 $total_code[ 10 ] = '';
321 if ($propType[ 'step' ] == 0) {
322 $total_code[ 13 ] = $total_code[ 15 ] = '';
323 if ($propValue[ 'step' ] == 1 || $propValue[ 'step' ] == - 1) {
324 $total_code[ 2 ] = $total_code[ 12 ] = $total_code[ 14 ] = $total_code[ 16 ] = '';
325 } elseif ($propValue[ 'step' ] < 0) {
326 $total_code[ 14 ] = - $propValue[ 'step' ];
328 $total_code[ 4 ] = '';
329 if ($propValue[ 'step' ] > 0) {
330 $total_code[ 8 ] = $total_code[ 9 ] = $total_code[ 10 ] = '';
332 $total_code[ 5 ] = $total_code[ 6 ] = $total_code[ 7 ] = $total_code[ 8 ] = '';
335 $propValue[ 'total' ] = join('', $total_code);
339 if (isset($namedAttr[ 'loop' ])) {
340 $initNamedProperty[ 'loop' ] = "'loop' => {$propValue['total']}";
342 if (isset($namedAttr[ 'total' ])) {
343 $initNamedProperty[ 'total' ] = "'total' => {$propValue['total']}";
344 if ($propType[ 'total' ] > 0) {
345 $propValue[ 'total' ] = "{$sectionVar}->value['total']";
347 } elseif ($propType[ 'total' ] > 0) {
348 $initLocal[ 'total' ] = $propValue[ 'total' ];
349 $propValue[ 'total' ] = "{$local}total";
352 $cmpFor[ 'iteration' ] = "{$propValue['iteration']} <= {$propValue['total']}";
354 foreach ($initLocal as $key => $code) {
355 $output .= "{$local}{$key} = {$code};\n";
358 $_vars = 'array(' . join(', ', $initNamedProperty) . ')';
359 $output .= "{$sectionVar} = new Smarty_Variable({$_vars});\n";
360 $cond_code = "{$propValue['total']} != 0";
361 if ($propType[ 'total' ] == 0) {
362 if ($propValue[ 'total' ] == 0) {
363 $cond_code = 'false';
368 if ($propType[ 'show' ] > 0) {
369 $output .= "{$local}show = {$propValue['show']} ? {$cond_code} : false;\n";
370 $output .= "if ({$local}show) {\n";
371 } elseif ($propValue[ 'show' ] == 'true') {
372 $output .= "if ({$cond_code}) {\n";
374 $output .= "if (false) {\n";
376 $jinit = join(', ', $initFor);
377 $jcmp = join(', ', $cmpFor);
378 $jinc = join(', ', $incFor);
379 $output .= "for ({$jinit}; {$jcmp}; {$jinc}){\n";
380 if (isset($namedAttr[ 'rownum' ])) {
381 $output .= "{$sectionVar}->value['rownum'] = {$propValue['iteration']};\n";
383 if (isset($namedAttr[ 'index_prev' ])) {
384 $output .= "{$sectionVar}->value['index_prev'] = {$propValue['index']} - {$propValue['step']};\n";
386 if (isset($namedAttr[ 'index_next' ])) {
387 $output .= "{$sectionVar}->value['index_next'] = {$propValue['index']} + {$propValue['step']};\n";
389 if (isset($namedAttr[ 'first' ])) {
390 $output .= "{$sectionVar}->value['first'] = ({$propValue['iteration']} == 1);\n";
392 if (isset($namedAttr[ 'last' ])) {
393 $output .= "{$sectionVar}->value['last'] = ({$propValue['iteration']} == {$propValue['total']});\n";
402 * Smarty Internal Plugin Compile Sectionelse Class
405 * @subpackage Compiler
407 class Smarty_Internal_Compile_Sectionelse extends Smarty_Internal_CompileBase
410 * Compiles code for the {sectionelse} tag
412 * @param array $args array with attributes from parser
413 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
415 * @return string compiled code
417 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
419 // check and get attributes
420 $_attr = $this->getAttributes($compiler, $args);
422 list($openTag, $nocache, $local, $sectionVar) = $this->closeTag($compiler, array('section'));
423 $this->openTag($compiler, 'sectionelse', array('sectionelse', $nocache, $local, $sectionVar));
425 return "<?php }} else {\n ?>";
430 * Smarty Internal Plugin Compile Sectionclose Class
433 * @subpackage Compiler
435 class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase
438 * Compiles code for the {/section} tag
440 * @param array $args array with attributes from parser
441 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
443 * @return string compiled code
445 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
447 $compiler->loopNesting --;
448 // must endblock be nocache?
449 if ($compiler->nocache) {
450 $compiler->tag_nocache = true;
453 list($openTag, $compiler->nocache, $local, $sectionVar) =
454 $this->closeTag($compiler, array('section', 'sectionelse'));
457 if ($openTag == 'sectionelse') {
462 $output .= "if ({$local}saved) {\n";
463 $output .= "{$sectionVar} = {$local}saved;\n";