3 * Smarty Internal Plugin Compile For
4 * Compiles the {for} {forelse} {/for} tags
12 * Smarty Internal Plugin Compile For Class
15 * @subpackage Compiler
17 class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase
20 * Compiles code for the {for} tag
21 * Smarty 3 does implement two different syntax's:
22 * - {for $var in $array}
23 * For looping over arrays or iterators
24 * - {for $x=0; $x<$y; $x++}
26 * The parser is generating different sets of attribute by which this compiler can
27 * determine which syntax is used.
29 * @param array $args array with attributes from parser
30 * @param object $compiler compiler object
31 * @param array $parameter array with compilation parameter
33 * @return string compiled code
35 public function compile($args, $compiler, $parameter)
37 if ($parameter == 0) {
38 $this->required_attributes = array('start', 'to');
39 $this->optional_attributes = array('max', 'step');
41 $this->required_attributes = array('start', 'ifexp', 'var', 'step');
42 $this->optional_attributes = array();
44 // check and get attributes
45 $_attr = $this->getAttributes($compiler, $args);
48 if ($parameter == 1) {
49 foreach ($_attr['start'] as $_statement) {
50 $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
51 $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value];\n";
53 $output .= " if ($_attr[ifexp]) { for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$_attr[var]]->value$_attr[step]) {\n";
55 $_statement = $_attr['start'];
56 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
57 if (isset($_attr['step'])) {
58 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = $_attr[step];";
60 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = 1;";
62 if (isset($_attr['max'])) {
63 $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";
65 $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";
67 $output .= "if (\$_smarty_tpl->tpl_vars[$_statement[var]]->total > 0) {\n";
68 $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";
69 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->first = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == 1;";
70 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->last = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == \$_smarty_tpl->tpl_vars[$_statement[var]]->total;";
74 $this->openTag($compiler, 'for', array('for', $compiler->nocache));
75 // maybe nocache because of nocache variables
76 $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
77 // return compiled code
83 * Smarty Internal Plugin Compile Forelse Class
86 * @subpackage Compiler
88 class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase
91 * Compiles code for the {forelse} tag
93 * @param array $args array with attributes from parser
94 * @param object $compiler compiler object
95 * @param array $parameter array with compilation parameter
97 * @return string compiled code
99 public function compile($args, $compiler, $parameter)
101 // check and get attributes
102 $_attr = $this->getAttributes($compiler, $args);
104 list($openTag, $nocache) = $this->closeTag($compiler, array('for'));
105 $this->openTag($compiler, 'forelse', array('forelse', $nocache));
107 return "<?php }} else { ?>";
112 * Smarty Internal Plugin Compile Forclose Class
115 * @subpackage Compiler
117 class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase
120 * Compiles code for the {/for} tag
122 * @param array $args array with attributes from parser
123 * @param object $compiler compiler object
124 * @param array $parameter array with compilation parameter
126 * @return string compiled code
128 public function compile($args, $compiler, $parameter)
130 // check and get attributes
131 $_attr = $this->getAttributes($compiler, $args);
132 // must endblock be nocache?
133 if ($compiler->nocache) {
134 $compiler->tag_nocache = true;
137 list($openTag, $compiler->nocache) = $this->closeTag($compiler, array('for', 'forelse'));
139 if ($openTag == 'forelse') {
142 return "<?php }} ?>";