3 * Smarty Internal Plugin Compile If
4 * Compiles the {if} {else} {elseif} {/if} tags
12 * Smarty Internal Plugin Compile If Class
15 * @subpackage Compiler
17 class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase
20 * Compiles code for the {if} tag
22 * @param array $args array with attributes from parser
23 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
24 * @param array $parameter array with compilation parameter
26 * @return string compiled code
27 * @throws \SmartyCompilerException
29 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
31 // check and get attributes
32 $_attr = $this->getAttributes($compiler, $args);
33 $this->openTag($compiler, 'if', array(1, $compiler->nocache));
34 // must whole block be nocache ?
35 $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
37 if (!array_key_exists("if condition", $parameter)) {
38 $compiler->trigger_template_error("missing if condition", null, true);
41 if (is_array($parameter[ 'if condition' ])) {
42 if (is_array($parameter[ 'if condition' ][ 'var' ])) {
43 $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ];
45 $var = $parameter[ 'if condition' ][ 'var' ];
47 if ($compiler->nocache) {
48 // create nocache var to make it know for further compiling
49 $compiler->setNocacheInVariable($var);
51 $prefixVar = $compiler->getNewPrefixVariable();
52 $_output = "<?php {$prefixVar} = " . $parameter[ 'if condition' ][ 'value' ] . ";?>\n";
53 $assignAttr = array();
54 $assignAttr[][ 'value' ] = "{$prefixVar}";
55 $assignCompiler = new Smarty_Internal_Compile_Assign();
56 if (is_array($parameter[ 'if condition' ][ 'var' ])) {
57 $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ][ 'var' ];
58 $_output .= $assignCompiler->compile($assignAttr, $compiler,
59 array('smarty_internal_index' => $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ]));
61 $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ];
62 $_output .= $assignCompiler->compile($assignAttr, $compiler, array());
64 $_output .= "<?php if ({$prefixVar}) {?>";
67 return "<?php if ({$parameter['if condition']}) {?>";
73 * Smarty Internal Plugin Compile Else Class
76 * @subpackage Compiler
78 class Smarty_Internal_Compile_Else extends Smarty_Internal_CompileBase
81 * Compiles code for the {else} tag
83 * @param array $args array with attributes from parser
84 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
85 * @param array $parameter array with compilation parameter
87 * @return string compiled code
89 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
91 list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
92 $this->openTag($compiler, 'else', array($nesting, $compiler->tag_nocache));
94 return "<?php } else { ?>";
99 * Smarty Internal Plugin Compile ElseIf Class
102 * @subpackage Compiler
104 class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase
107 * Compiles code for the {elseif} tag
109 * @param array $args array with attributes from parser
110 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
111 * @param array $parameter array with compilation parameter
113 * @return string compiled code
114 * @throws \SmartyCompilerException
116 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
118 // check and get attributes
119 $_attr = $this->getAttributes($compiler, $args);
121 list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
123 if (!array_key_exists("if condition", $parameter)) {
124 $compiler->trigger_template_error("missing elseif condition", null, true);
129 if (is_array($parameter[ 'if condition' ])) {
130 $condition_by_assign = true;
131 if (is_array($parameter[ 'if condition' ][ 'var' ])) {
132 $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ];
134 $var = $parameter[ 'if condition' ][ 'var' ];
136 if ($compiler->nocache) {
137 // create nocache var to make it know for further compiling
138 $compiler->setNocacheInVariable($var);
140 $prefixVar = $compiler->getNewPrefixVariable();
141 $assignCode = "<?php {$prefixVar} = " . $parameter[ 'if condition' ][ 'value' ] . ";?>\n";
142 $assignCompiler = new Smarty_Internal_Compile_Assign();
143 $assignAttr = array();
144 $assignAttr[][ 'value' ] = "{$prefixVar}";
145 if (is_array($parameter[ 'if condition' ][ 'var' ])) {
146 $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ][ 'var' ];
147 $assignCode .= $assignCompiler->compile($assignAttr, $compiler,
148 array('smarty_internal_index' => $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ]));
150 $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ];
151 $assignCode .= $assignCompiler->compile($assignAttr, $compiler, array());
154 $condition_by_assign = false;
157 $prefixCode = $compiler->getPrefixCode();
158 if (empty($prefixCode)) {
159 if ($condition_by_assign) {
160 $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
161 $_output = $compiler->appendCode("<?php } else {\n?>", $assignCode);
162 return $compiler->appendCode($_output, "<?php if ({$prefixVar}) {?>");
164 $this->openTag($compiler, 'elseif', array($nesting, $compiler->tag_nocache));
165 return "<?php } elseif ({$parameter['if condition']}) {?>";
168 $_output = $compiler->appendCode("<?php } else {\n?>", $prefixCode);
169 $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
170 if ($condition_by_assign) {
171 $_output = $compiler->appendCode($_output, $assignCode);
172 return $compiler->appendCode($_output, "<?php if ({$prefixVar}) {?>");
174 return $compiler->appendCode($_output, "<?php if ({$parameter['if condition']}) {?>");
181 * Smarty Internal Plugin Compile Ifclose Class
184 * @subpackage Compiler
186 class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase
189 * Compiles code for the {/if} tag
191 * @param array $args array with attributes from parser
192 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
193 * @param array $parameter array with compilation parameter
195 * @return string compiled code
197 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
199 // must endblock be nocache?
200 if ($compiler->nocache) {
201 $compiler->tag_nocache = true;
203 list($nesting, $compiler->nocache) = $this->closeTag($compiler, array('if', 'else', 'elseif'));
205 for ($i = 0; $i < $nesting; $i ++) {
209 return "<?php {$tmp}?>";