]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/sysplugins/smarty_internal_compile_for.php
Changes in api
[friendica.git] / library / Smarty / libs / sysplugins / smarty_internal_compile_for.php
1 <?php
2 /**
3  * Smarty Internal Plugin Compile For
4  * Compiles the {for} {forelse} {/for} tags
5  *
6  * @package    Smarty
7  * @subpackage Compiler
8  * @author     Uwe Tews
9  */
10
11 /**
12  * Smarty Internal Plugin Compile For Class
13  *
14  * @package    Smarty
15  * @subpackage Compiler
16  */
17 class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase
18 {
19     /**
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++}
25      * For general loops
26      * The parser is generating different sets of attribute by which this compiler can
27      * determine which syntax is used.
28      *
29      * @param  array  $args      array with attributes from parser
30      * @param  object $compiler  compiler object
31      * @param  array  $parameter array with compilation parameter
32      *
33      * @return string compiled code
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         // check and get attributes
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         // maybe nocache because of nocache variables
76         $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
77         // return compiled code
78         return $output;
79     }
80 }
81
82 /**
83  * Smarty Internal Plugin Compile Forelse Class
84  *
85  * @package    Smarty
86  * @subpackage Compiler
87  */
88 class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase
89 {
90     /**
91      * Compiles code for the {forelse} tag
92      *
93      * @param  array  $args      array with attributes from parser
94      * @param  object $compiler  compiler object
95      * @param  array  $parameter array with compilation parameter
96      *
97      * @return string compiled code
98      */
99     public function compile($args, $compiler, $parameter)
100     {
101         // check and get attributes
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  * Smarty Internal Plugin Compile Forclose Class
113  *
114  * @package    Smarty
115  * @subpackage Compiler
116  */
117 class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase
118 {
119     /**
120      * Compiles code for the {/for} tag
121      *
122      * @param  array  $args      array with attributes from parser
123      * @param  object $compiler  compiler object
124      * @param  array  $parameter array with compilation parameter
125      *
126      * @return string compiled code
127      */
128     public function compile($args, $compiler, $parameter)
129     {
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;
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 }