1 <?php
2 /**
3 * Smarty Internal Plugin Compile Section
4 *
5 * Compiles the {section} {sectionelse} {/section} tags
6 *
7 * @package Smarty
8 * @subpackage Compiler
9 * @author Uwe Tews
10 */
11 /**
12 * Smarty Internal Plugin Compile Section Class
13 */
14 class Smarty_Internal_Compile_Section extends Smarty_Internal_CompileBase {
15 /**
16 * Compiles code for the {section} tag
17 *
18 * @param array $args array with attributes from parser
19 * @param object $compiler compiler object
20 * @return string compiled code
21 */
22 public function compile($args, $compiler)
23 {
24 $this->compiler = $compiler;
25 $this->required_attributes = array('name', 'loop');
26 $this->optional_attributes = array('start', 'step', 'max', 'show');
27 // check and get attributes
28 $_attr = $this->_get_attributes($args);
30 $this->_open_tag('section', array('section',$this->compiler->nocache));
31 // maybe nocache because of nocache variables
32 $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
34 $output = "<?php ";
36 $section_name = $_attr['name'];
38 $output .= "unset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]);\n";
39 $section_props = "\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]";
41 foreach ($_attr as $attr_name => $attr_value) {
42 switch ($attr_name) {
43 case 'loop':
44 $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";
45 break;
47 case 'show':
48 if (is_bool($attr_value))
49 $show_attr_value = $attr_value ? 'true' : 'false';
50 else
51 $show_attr_value = "(bool)$attr_value";
52 $output .= "{$section_props}['show'] = $show_attr_value;\n";
53 break;
55 case 'name':
56 $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
57 break;
59 case 'max':
60 case 'start':
61 $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
62 break;
64 case 'step':
65 $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
66 break;
67 }
68 }
70 if (!isset($_attr['show']))
71 $output .= "{$section_props}['show'] = true;\n";
73 if (!isset($_attr['loop']))
74 $output .= "{$section_props}['loop'] = 1;\n";
76 if (!isset($_attr['max']))
77 $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
78 else
79 $output .= "if ({$section_props}['max'] < 0)\n" . " {$section_props}['max'] = {$section_props}['loop'];\n";
81 if (!isset($_attr['step']))
82 $output .= "{$section_props}['step'] = 1;\n";
84 if (!isset($_attr['start']))
85 $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
86 else {
87 $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";
88 }
90 $output .= "if ({$section_props}['show']) {\n";
91 if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) {
92 $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
93 } else {
94 $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";
95 }
96 $output .= " if ({$section_props}['total'] == 0)\n" . " {$section_props}['show'] = false;\n" . "} else\n" . " {$section_props}['total'] = 0;\n";
98 $output .= "if ({$section_props}['show']):\n";
99 $output .= "
100 for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
101 {$section_props}['iteration'] <= {$section_props}['total'];
102 {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
103 $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
104 $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
105 $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
106 $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
107 $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
109 $output .= "?>";
110 return $output;
111 }
112 }
114 /**
115 * Smarty Internal Plugin Compile Sectionelse Class
116 */
117 class Smarty_Internal_Compile_Sectionelse extends Smarty_Internal_CompileBase {
118 /**
119 * Compiles code for the {sectionelse} tag
120 *
121 * @param array $args array with attributes from parser
122 * @param object $compiler compiler object
123 * @return string compiled code
124 */
125 public function compile($args, $compiler)
126 {
127 $this->compiler = $compiler;
128 // check and get attributes
129 $_attr = $this->_get_attributes($args);
131 list($_open_tag, $nocache) = $this->_close_tag(array('section'));
132 $this->_open_tag('sectionelse',array('sectionelse', $nocache));
134 return "<?php endfor; else: ?>";
135 }
136 }
138 /**
139 * Smarty Internal Plugin Compile Sectionclose Class
140 */
141 class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase {
142 /**
143 * Compiles code for the {/section} tag
144 *
145 * @param array $args array with attributes from parser
146 * @param object $compiler compiler object
147 * @return string compiled code
148 */
149 public function compile($args, $compiler)
150 {
151 $this->compiler = $compiler;
152 // check and get attributes
153 $_attr = $this->_get_attributes($args);
155 // must endblock be nocache?
156 if ($this->compiler->nocache) {
157 $this->compiler->tag_nocache = true;
158 }
160 list($_open_tag, $this->compiler->nocache) = $this->_close_tag(array('section', 'sectionelse'));
162 if ($_open_tag == 'sectionelse')
163 return "<?php endif; ?>";
164 else
165 return "<?php endfor; endif; ?>";
166 }
167 }
170 ?>