3 * Smarty Internal Plugin Compile Section
5 * Compiles the {section} {sectionelse} {/section} tags
13 * Smarty Internal Plugin Compile Section Class
15 class Smarty_Internal_Compile_Section extends Smarty_Internal_CompileBase {
16 // attribute definitions
17 public $required_attributes = array('name', 'loop');
18 public $shorttag_order = array('name', 'loop');
19 public $optional_attributes = array('start', 'step', 'max', 'show');
22 * Compiles code for the {section} tag
24 * @param array $args array with attributes from parser
25 * @param object $compiler compiler object
26 * @return string compiled code
28 public function compile($args, $compiler)
30 $this->compiler = $compiler;
31 // check and get attributes
32 $_attr = $this->_get_attributes($args);
34 $this->_open_tag('section', array('section',$this->compiler->nocache));
35 // maybe nocache because of nocache variables
36 $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
40 $section_name = $_attr['name'];
42 $output .= "unset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]);\n";
43 $section_props = "\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]";
45 foreach ($_attr as $attr_name => $attr_value) {
48 $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";
52 if (is_bool($attr_value))
53 $show_attr_value = $attr_value ? 'true' : 'false';
55 $show_attr_value = "(bool)$attr_value";
56 $output .= "{$section_props}['show'] = $show_attr_value;\n";
60 $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
65 $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
69 $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
74 if (!isset($_attr['show']))
75 $output .= "{$section_props}['show'] = true;\n";
77 if (!isset($_attr['loop']))
78 $output .= "{$section_props}['loop'] = 1;\n";
80 if (!isset($_attr['max']))
81 $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
83 $output .= "if ({$section_props}['max'] < 0)\n" . " {$section_props}['max'] = {$section_props}['loop'];\n";
85 if (!isset($_attr['step']))
86 $output .= "{$section_props}['step'] = 1;\n";
88 if (!isset($_attr['start']))
89 $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
91 $output .= "if ({$section_props}['start'] < 0)\n" . " {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" . "else\n" . " {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
94 $output .= "if ({$section_props}['show']) {\n";
95 if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) {
96 $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
98 $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";
100 $output .= " if ({$section_props}['total'] == 0)\n" . " {$section_props}['show'] = false;\n" . "} else\n" . " {$section_props}['total'] = 0;\n";
102 $output .= "if ({$section_props}['show']):\n";
104 for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
105 {$section_props}['iteration'] <= {$section_props}['total'];
106 {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
107 $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
108 $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
109 $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
110 $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
111 $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
119 * Smarty Internal Plugin Compile Sectionelse Class
121 class Smarty_Internal_Compile_Sectionelse extends Smarty_Internal_CompileBase {
123 * Compiles code for the {sectionelse} tag
125 * @param array $args array with attributes from parser
126 * @param object $compiler compiler object
127 * @return string compiled code
129 public function compile($args, $compiler)
131 $this->compiler = $compiler;
132 // check and get attributes
133 $_attr = $this->_get_attributes($args);
135 list($_open_tag, $nocache) = $this->_close_tag(array('section'));
136 $this->_open_tag('sectionelse',array('sectionelse', $nocache));
138 return "<?php endfor; else: ?>";
143 * Smarty Internal Plugin Compile Sectionclose Class
145 class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase {
147 * Compiles code for the {/section} tag
149 * @param array $args array with attributes from parser
150 * @param object $compiler compiler object
151 * @return string compiled code
153 public function compile($args, $compiler)
155 $this->compiler = $compiler;
156 // check and get attributes
157 $_attr = $this->_get_attributes($args);
159 // must endblock be nocache?
160 if ($this->compiler->nocache) {
161 $this->compiler->tag_nocache = true;
164 list($_open_tag, $this->compiler->nocache) = $this->_close_tag(array('section', 'sectionelse'));
166 if ($_open_tag == 'sectionelse')
167 return "<?php endif; ?>";
169 return "<?php endfor; endif; ?>";