3 * Template Lite section_start compile plugin converted from Smarty
9 function compile_section_start($arguments, &$object)
11 $attrs = $object->_parse_arguments($arguments);
15 $section_name = $attrs['name'];
16 if (empty($section_name))
18 throw new Template_Exception("missing section name", $object);
21 $output .= "if (isset(\$this->_sections['$section_name'])) unset(\$this->_sections['$section_name']);\n";
22 $section_props = "\$this->_sections['$section_name']";
24 foreach ($attrs as $attr_name => $attr_value)
29 $output .= "{$section_props}['loop'] = is_array($attr_value) ? count($attr_value) : max(0, (int)$attr_value);\n";
33 if (is_bool($attr_value))
35 $show_attr_value = $attr_value ?
'true' : 'false';
39 $show_attr_value = "(bool)$attr_value";
41 $output .= "{$section_props}['show'] = $show_attr_value;\n";
45 $output .= "{$section_props}['$attr_name'] = '$attr_value';\n";
50 $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
54 $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
58 throw new Template_Exception("unknown section attribute - '$attr_name'", $object);
63 if (!isset($attrs['show']))
65 $output .= "{$section_props}['show'] = true;\n";
68 if (!isset($attrs['loop']))
70 $output .= "{$section_props}['loop'] = 1;\n";
73 if (!isset($attrs['max']))
75 $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
79 $output .= "if ({$section_props}['max'] < 0)\n" .
80 " {$section_props}['max'] = {$section_props}['loop'];\n";
83 if (!isset($attrs['step']))
85 $output .= "{$section_props}['step'] = 1;\n";
88 if (!isset($attrs['start']))
90 $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
94 $output .= "if ({$section_props}['start'] < 0)\n" .
95 " {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" .
97 " {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
100 $output .= "if ({$section_props}['show']) {\n";
101 if (!isset($attrs['start']) && !isset($attrs['step']) && !isset($attrs['max']))
103 $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
107 $output .= " {$section_props}['total'] = min(ceil(({$section_props}['step'] > 0 ? {$section_props}['loop'] - {$section_props}['start'] : {$section_props}['start']+1)/abs({$section_props}['step'])), {$section_props}['max']);\n";
109 $output .= " if ({$section_props}['total'] == 0)\n" .
110 " {$section_props}['show'] = false;\n" .
112 " {$section_props}['total'] = 0;\n";
114 $output .= "if ({$section_props}['show']):\n";
116 for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
117 {$section_props}['iteration'] <= {$section_props}['total'];
118 {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
119 $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
120 $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
121 $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
122 $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
123 $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";