source: trunk/include/smarty/libs/sysplugins/smarty_internal_compile_section.php @ 23485

Last change on this file since 23485 was 23485, checked in by rvelices, 11 years ago

Smarty EOL style LF svn property

  • Property svn:eol-style set to LF
File size: 7.3 KB
Line 
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/**
13 * Smarty Internal Plugin Compile Section Class
14 *
15 * @package Smarty
16 * @subpackage Compiler
17 */
18class Smarty_Internal_Compile_Section extends Smarty_Internal_CompileBase {
19
20    /**
21     * Attribute definition: Overwrites base class.
22     *
23     * @var array
24     * @see Smarty_Internal_CompileBase
25     */
26    public $required_attributes = array('name', 'loop');
27    /**
28     * Attribute definition: Overwrites base class.
29     *
30     * @var array
31     * @see Smarty_Internal_CompileBase
32     */
33    public $shorttag_order = array('name', 'loop');
34    /**
35     * Attribute definition: Overwrites base class.
36     *
37     * @var array
38     * @see Smarty_Internal_CompileBase
39     */
40    public $optional_attributes = array('start', 'step', 'max', 'show');
41
42    /**
43     * Compiles code for the {section} tag
44     *
45     * @param array  $args     array with attributes from parser
46     * @param object $compiler compiler object
47     * @return string compiled code
48     */
49    public function compile($args, $compiler)
50    {
51        // check and get attributes
52        $_attr = $this->getAttributes($compiler, $args);
53
54        $this->openTag($compiler, 'section', array('section', $compiler->nocache));
55        // maybe nocache because of nocache variables
56        $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
57
58        $output = "<?php ";
59
60        $section_name = $_attr['name'];
61
62        $output .= "if (isset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name])) unset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]);\n";
63        $section_props = "\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]";
64
65        foreach ($_attr as $attr_name => $attr_value) {
66            switch ($attr_name) {
67                case 'loop':
68                    $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";
69                    break;
70
71                case 'show':
72                    if (is_bool($attr_value))
73                        $show_attr_value = $attr_value ? 'true' : 'false';
74                    else
75                        $show_attr_value = "(bool)$attr_value";
76                    $output .= "{$section_props}['show'] = $show_attr_value;\n";
77                    break;
78
79                case 'name':
80                    $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
81                    break;
82
83                case 'max':
84                case 'start':
85                    $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
86                    break;
87
88                case 'step':
89                    $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
90                    break;
91            }
92        }
93
94        if (!isset($_attr['show']))
95            $output .= "{$section_props}['show'] = true;\n";
96
97        if (!isset($_attr['loop']))
98            $output .= "{$section_props}['loop'] = 1;\n";
99
100        if (!isset($_attr['max']))
101            $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
102        else
103            $output .= "if ({$section_props}['max'] < 0)\n" . "    {$section_props}['max'] = {$section_props}['loop'];\n";
104
105        if (!isset($_attr['step']))
106            $output .= "{$section_props}['step'] = 1;\n";
107
108        if (!isset($_attr['start']))
109            $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
110        else {
111            $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";
112        }
113
114        $output .= "if ({$section_props}['show']) {\n";
115        if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) {
116            $output .= "    {$section_props}['total'] = {$section_props}['loop'];\n";
117        } else {
118            $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";
119        }
120        $output .= "    if ({$section_props}['total'] == 0)\n" . "        {$section_props}['show'] = false;\n" . "} else\n" . "    {$section_props}['total'] = 0;\n";
121
122        $output .= "if ({$section_props}['show']):\n";
123        $output .= "
124            for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
125                 {$section_props}['iteration'] <= {$section_props}['total'];
126                 {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
127        $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
128        $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
129        $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
130        $output .= "{$section_props}['first']      = ({$section_props}['iteration'] == 1);\n";
131        $output .= "{$section_props}['last']       = ({$section_props}['iteration'] == {$section_props}['total']);\n";
132
133        $output .= "?>";
134        return $output;
135    }
136
137}
138
139/**
140 * Smarty Internal Plugin Compile Sectionelse Class
141 *
142 * @package Smarty
143 * @subpackage Compiler
144 */
145class Smarty_Internal_Compile_Sectionelse extends Smarty_Internal_CompileBase {
146
147    /**
148     * Compiles code for the {sectionelse} tag
149     *
150     * @param array  $args     array with attributes from parser
151     * @param object $compiler compiler object
152     * @return string compiled code
153     */
154    public function compile($args, $compiler)
155    {
156        // check and get attributes
157        $_attr = $this->getAttributes($compiler, $args);
158
159        list($openTag, $nocache) = $this->closeTag($compiler, array('section'));
160        $this->openTag($compiler, 'sectionelse', array('sectionelse', $nocache));
161
162        return "<?php endfor; else: ?>";
163    }
164
165}
166
167/**
168 * Smarty Internal Plugin Compile Sectionclose Class
169 *
170 * @package Smarty
171 * @subpackage Compiler
172 */
173class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase {
174
175    /**
176     * Compiles code for the {/section} tag
177     *
178     * @param array  $args     array with attributes from parser
179     * @param object $compiler compiler object
180     * @return string compiled code
181     */
182    public function compile($args, $compiler)
183    {
184        // check and get attributes
185        $_attr = $this->getAttributes($compiler, $args);
186
187        // must endblock be nocache?
188        if ($compiler->nocache) {
189            $compiler->tag_nocache = true;
190        }
191
192        list($openTag, $compiler->nocache) = $this->closeTag($compiler, array('section', 'sectionelse'));
193
194        if ($openTag == 'sectionelse') {
195            return "<?php endif; ?>";
196        } else {
197            return "<?php endfor; endif; ?>";
198        }
199    }
200
201}
202
203?>
Note: See TracBrowser for help on using the repository browser.