1 <?php
2 /**
3 * Smarty Internal Plugin Compile For
4 *
5 * Compiles the {for} {forelse} {/for} tags
6 *
7 * @package Smarty
8 * @subpackage Compiler
9 * @author Uwe Tews
10 */
11 /**
12 * Smarty Internal Plugin Compile For Class
13 */
14 class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase {
15 /**
16 * Compiles code for the {for} tag
17 *
18 * Smarty 3 does implement two different sytaxes:
19 *
20 * - {for $var in $array}
21 * For looping over arrays or iterators
22 *
23 * - {for $x=0; $x<$y; $x++}
24 * For general loops
25 *
26 * The parser is gereration different sets of attribute by which this compiler can
27 * determin which syntax is used.
28 *
29 * @param array $args array with attributes from parser
30 * @param object $compiler compiler object
31 * @return string compiled code
32 */
33 public function compile($args, $compiler)
34 {
35 $this->compiler = $compiler;
36 // {for $x=0; $x<$y; $x++} syntax
37 if (isset($args['ifexp'])) {
38 $this->required_attributes = array('ifexp', 'start', 'loop', 'varloop');
39 } else {
40 $this->required_attributes = array('start', 'to');
41 $this->optional_attributes = array('step', 'max');
42 }
43 // check and get attributes
44 $_attr = $this->_get_attributes($args);
46 $this->_open_tag('for', array('for', $this->compiler->nocache));
47 // maybe nocache because of nocache variables
48 $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
50 $output = "<?php ";
51 if (isset($_attr['ifexp'])) {
52 foreach ($_attr['start'] as $_statement) {
53 $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
54 $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value];\n";
55 $compiler->local_var[$_statement['var']] = true;
56 }
57 $output .= " if ($_attr[ifexp]){ for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$_attr[varloop]]->value$_attr[loop]){\n";
58 } else {
59 $_statement = $_attr['start'];
60 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
61 $compiler->local_var[$_statement['var']] = true;
62 if (isset($_attr['step'])) {
63 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = $_attr[step];";
64 } else {
65 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = ($_attr[to] - ($_statement[value]) < 0) ? -1 : 1;";
66 }
67 if (isset($_attr['max'])) {
68 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->total = (int)min(ceil((\$_smarty_tpl->tpl_vars[$_statement[var]]->step > 0 ? $_attr[to]+1 - $_statement[value] : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$_statement[var]]->step)),$_attr[max]);\n";
69 } else {
70 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->total = (int)ceil((\$_smarty_tpl->tpl_vars[$_statement[var]]->step > 0 ? $_attr[to]+1 - $_statement[value] : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$_statement[var]]->step));\n";
71 }
72 $output .= "if (\$_smarty_tpl->tpl_vars[$_statement[var]]->total > 0){\n";
73 $output .= "for (\$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value], \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration = 1;\$_smarty_tpl->tpl_vars[$_statement[var]]->iteration <= \$_smarty_tpl->tpl_vars[$_statement[var]]->total;\$_smarty_tpl->tpl_vars[$_statement[var]]->value += \$_smarty_tpl->tpl_vars[$_statement[var]]->step, \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration++){\n";
74 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->first = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == 1;";
75 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->last = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == \$_smarty_tpl->tpl_vars[$_statement[var]]->total;";
76 }
77 $output .= "?>";
78 // return compiled code
79 return $output;
80 }
81 }
83 /**
84 * Smarty Internal Plugin Compile Forelse Class
85 */
86 class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase {
87 /**
88 * Compiles code for the {forelse} tag
89 *
90 * @param array $args array with attributes from parser
91 * @param object $compiler compiler object
92 * @return string compiled code
93 */
94 public function compile($args, $compiler)
95 {
96 $this->compiler = $compiler;
97 // check and get attributes
98 $_attr = $this->_get_attributes($args);
100 list($_open_tag, $nocache) = $this->_close_tag(array('for'));
101 $this->_open_tag('forelse', array('forelse', $nocache));
102 return "<?php }} else { ?>";
103 }
104 }
106 /**
107 * Smarty Internal Plugin Compile Forclose Class
108 */
109 class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase {
110 /**
111 * Compiles code for the {/for} tag
112 *
113 * @param array $args array with attributes from parser
114 * @param object $compiler compiler object
115 * @return string compiled code
116 */
117 public function compile($args, $compiler)
118 {
119 $this->compiler = $compiler;
120 // check and get attributes
121 $_attr = $this->_get_attributes($args);
122 // must endblock be nocache?
123 if ($this->compiler->nocache) {
124 $this->compiler->tag_nocache = true;
125 }
127 list($_open_tag, $this->compiler->nocache) = $this->_close_tag(array('for', 'forelse'));
128 if ($_open_tag == 'forelse')
129 return "<?php } ?>";
130 else
131 return "<?php }} ?>";
132 }
133 }
135 ?>