source: trunk/include/smarty/libs/sysplugins/smarty_internal_compile_if.php @ 23384

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

smarty 3 - first pass for tests

File size: 9.0 KB
Line 
1<?php
2/**
3* Smarty Internal Plugin Compile If
4*
5* Compiles the {if} {else} {elseif} {/if} tags
6*
7* @package Smarty
8* @subpackage Compiler
9* @author Uwe Tews
10*/
11
12/**
13* Smarty Internal Plugin Compile If Class
14*
15* @package Smarty
16* @subpackage Compiler
17*/
18class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase {
19
20    /**
21    * Compiles code for the {if} tag
22    *
23    * @param array  $args       array with attributes from parser
24    * @param object $compiler   compiler object
25    * @param array  $parameter  array with compilation parameter
26    * @return string compiled code
27    */
28    public function compile($args, $compiler, $parameter)
29    {
30        // check and get attributes
31        $_attr = $this->getAttributes($compiler, $args);
32        $this->openTag($compiler, 'if', array(1, $compiler->nocache));
33        // must whole block be nocache ?
34        $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
35
36        if (!array_key_exists("if condition",$parameter)) {
37            $compiler->trigger_template_error("missing if condition", $compiler->lex->taglineno);
38        }
39
40        if (is_array($parameter['if condition'])) {
41            if ($compiler->nocache) {
42                $_nocache = ',true';
43                // create nocache var to make it know for further compiling
44                if (is_array($parameter['if condition']['var'])) {
45                    $compiler->template->tpl_vars[trim($parameter['if condition']['var']['var'], "'")] = new Smarty_variable(null, true);
46                } else {
47                    $compiler->template->tpl_vars[trim($parameter['if condition']['var'], "'")] = new Smarty_variable(null, true);
48                }
49            } else {
50                $_nocache = '';
51            }
52            if (is_array($parameter['if condition']['var'])) {
53                $_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']['var']."]) || !is_array(\$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']['var']."]->value)) \$_smarty_tpl->createLocalArrayVariable(".$parameter['if condition']['var']['var']."$_nocache);\n";
54                $_output .= "if (\$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']['var']."]->value".$parameter['if condition']['var']['smarty_internal_index']." = ".$parameter['if condition']['value']."){?>";
55            } else {
56                $_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']."])) \$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']."] = new Smarty_Variable(null{$_nocache});";
57                $_output .= "if (\$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']."]->value = ".$parameter['if condition']['value']."){?>";
58            }
59            return $_output;
60        } else {
61            return "<?php if ({$parameter['if condition']}){?>";
62        }
63    }
64
65}
66
67/**
68* Smarty Internal Plugin Compile Else Class
69*
70* @package Smarty
71* @subpackage Compiler
72*/
73class Smarty_Internal_Compile_Else extends Smarty_Internal_CompileBase {
74
75    /**
76    * Compiles code for the {else} tag
77    *
78    * @param array  $args       array with attributes from parser
79    * @param object $compiler   compiler object
80    * @param array  $parameter  array with compilation parameter
81    * @return string compiled code
82    */
83    public function compile($args, $compiler, $parameter)
84    {
85        list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
86        $this->openTag($compiler, 'else', array($nesting, $compiler->tag_nocache));
87
88        return "<?php }else{ ?>";
89    }
90
91}
92
93/**
94* Smarty Internal Plugin Compile ElseIf Class
95*
96* @package Smarty
97* @subpackage Compiler
98*/
99class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase {
100
101    /**
102    * Compiles code for the {elseif} tag
103    *
104    * @param array  $args       array with attributes from parser
105    * @param object $compiler   compiler object
106    * @param array  $parameter  array with compilation parameter
107    * @return string compiled code
108    */
109    public function compile($args, $compiler, $parameter)
110    {
111        // check and get attributes
112        $_attr = $this->getAttributes($compiler, $args);
113
114        list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
115
116        if (!array_key_exists("if condition",$parameter)) {
117            $compiler->trigger_template_error("missing elseif condition", $compiler->lex->taglineno);
118        }
119
120        if (is_array($parameter['if condition'])) {
121            $condition_by_assign = true;
122            if ($compiler->nocache) {
123                $_nocache = ',true';
124                // create nocache var to make it know for further compiling
125                if (is_array($parameter['if condition']['var'])) {
126                    $compiler->template->tpl_vars[trim($parameter['if condition']['var']['var'], "'")] = new Smarty_variable(null, true);
127                } else {
128                    $compiler->template->tpl_vars[trim($parameter['if condition']['var'], "'")] = new Smarty_variable(null, true);
129                }
130            } else {
131                $_nocache = '';
132            }
133        } else {
134            $condition_by_assign = false;
135        }
136
137        if (empty($compiler->prefix_code)) {
138            if ($condition_by_assign) {
139                $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
140                if (is_array($parameter['if condition']['var'])) {
141                    $_output = "<?php }else{ if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value)) \$_smarty_tpl->createLocalArrayVariable(" . $parameter['if condition']['var']['var'] . "$_nocache);\n";
142                    $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['value'] . "){?>";
143                } else {
144                    $_output = "<?php  }else{ if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "])) \$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "] = new Smarty_Variable(null{$_nocache});";
145                    $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . "){?>";
146                }
147                return $_output;
148            } else {
149                $this->openTag($compiler, 'elseif', array($nesting, $compiler->tag_nocache));
150                return "<?php }elseif({$parameter['if condition']}){?>";
151            }
152        } else {
153            $tmp = '';
154            foreach ($compiler->prefix_code as $code)
155            $tmp .= $code;
156            $compiler->prefix_code = array();
157            $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
158            if ($condition_by_assign) {
159                if (is_array($parameter['if condition']['var'])) {
160                    $_output = "<?php }else{?>{$tmp}<?php  if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value)) \$_smarty_tpl->createLocalArrayVariable(" . $parameter['if condition']['var']['var'] . "$_nocache);\n";
161                    $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['value'] . "){?>";
162                } else {
163                    $_output = "<?php }else{?>{$tmp}<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "])) \$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "] = new Smarty_Variable(null{$_nocache});";
164                    $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . "){?>";
165                }
166                return $_output;
167            } else {
168                return "<?php }else{?>{$tmp}<?php if ({$parameter['if condition']}){?>";
169            }
170        }
171    }
172
173}
174
175/**
176* Smarty Internal Plugin Compile Ifclose Class
177*
178* @package Smarty
179* @subpackage Compiler
180*/
181class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase {
182
183    /**
184    * Compiles code for the {/if} tag
185    *
186    * @param array  $args       array with attributes from parser
187    * @param object $compiler   compiler object
188    * @param array  $parameter  array with compilation parameter
189    * @return string compiled code
190    */
191    public function compile($args, $compiler, $parameter)
192    {
193        // must endblock be nocache?
194        if ($compiler->nocache) {
195            $compiler->tag_nocache = true;
196        }
197        list($nesting, $compiler->nocache) = $this->closeTag($compiler, array('if', 'else', 'elseif'));
198        $tmp = '';
199        for ($i = 0; $i < $nesting; $i++) {
200            $tmp .= '}';
201        }
202        return "<?php {$tmp}?>";
203    }
204
205}
206
207?>
Note: See TracBrowser for help on using the repository browser.