1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16:
17: class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase
18: {
19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
35: public function compile($args, $compiler, $parameter)
36: {
37: if ($parameter == 0) {
38: $this->required_attributes = array('start', 'to');
39: $this->optional_attributes = array('max', 'step');
40: } else {
41: $this->required_attributes = array('start', 'ifexp', 'var', 'step');
42: $this->optional_attributes = array();
43: }
44:
45: $_attr = $this->getAttributes($compiler, $args);
46:
47: $output = "<?php ";
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";
52: }
53: $output .= " if ($_attr[ifexp]) { for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$_attr[var]]->value$_attr[step]) {\n";
54: } else {
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];";
59: } else {
60: $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = 1;";
61: }
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";
64: } else {
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";
66: }
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;";
71: }
72: $output .= "?>";
73:
74: $this->openTag($compiler, 'for', array('for', $compiler->nocache));
75:
76: $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
77:
78: return $output;
79: }
80: }
81:
82: 83: 84: 85: 86: 87:
88: class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase
89: {
90: 91: 92: 93: 94: 95: 96: 97: 98:
99: public function compile($args, $compiler, $parameter)
100: {
101:
102: $_attr = $this->getAttributes($compiler, $args);
103:
104: list($openTag, $nocache) = $this->closeTag($compiler, array('for'));
105: $this->openTag($compiler, 'forelse', array('forelse', $nocache));
106:
107: return "<?php }} else { ?>";
108: }
109: }
110:
111: 112: 113: 114: 115: 116:
117: class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase
118: {
119: 120: 121: 122: 123: 124: 125: 126: 127:
128: public function compile($args, $compiler, $parameter)
129: {
130:
131: $_attr = $this->getAttributes($compiler, $args);
132:
133: if ($compiler->nocache) {
134: $compiler->tag_nocache = true;
135: }
136:
137: list($openTag, $compiler->nocache) = $this->closeTag($compiler, array('for', 'forelse'));
138:
139: if ($openTag == 'forelse') {
140: return "<?php } ?>";
141: } else {
142: return "<?php }} ?>";
143: }
144: }
145: }
146: