3 * Smarty Internal Plugin Compile If
5 * Compiles the {if} {else} {elseif} {/if} tags
13 * Smarty Internal Plugin Compile If Class
16 * @subpackage Compiler
18 class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase {
21 * Compiles code for the {if} tag
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
28 public function compile($args, $compiler, $parameter)
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;
36 if (!array_key_exists("if condition",$parameter)) {
37 $compiler->trigger_template_error("missing if condition", $compiler->lex->taglineno);
40 if (is_array($parameter['if condition'])) {
41 if ($compiler->nocache) {
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);
47 $compiler->template->tpl_vars[trim($parameter['if condition']['var'], "'")] = new Smarty_variable(null, true);
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']."){?>";
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']."){?>";
61 return "<?php if ({$parameter['if condition']}){?>";
68 * Smarty Internal Plugin Compile Else Class
71 * @subpackage Compiler
73 class Smarty_Internal_Compile_Else extends Smarty_Internal_CompileBase {
76 * Compiles code for the {else} tag
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
83 public function compile($args, $compiler, $parameter)
85 list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
86 $this->openTag($compiler, 'else', array($nesting, $compiler->tag_nocache));
88 return "<?php }else{ ?>";
94 * Smarty Internal Plugin Compile ElseIf Class
97 * @subpackage Compiler
99 class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase {
102 * Compiles code for the {elseif} tag
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
109 public function compile($args, $compiler, $parameter)
111 // check and get attributes
112 $_attr = $this->getAttributes($compiler, $args);
114 list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
116 if (!array_key_exists("if condition",$parameter)) {
117 $compiler->trigger_template_error("missing elseif condition", $compiler->lex->taglineno);
120 if (is_array($parameter['if condition'])) {
121 $condition_by_assign = true;
122 if ($compiler->nocache) {
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);
128 $compiler->template->tpl_vars[trim($parameter['if condition']['var'], "'")] = new Smarty_variable(null, true);
134 $condition_by_assign = false;
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'] . "){?>";
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'] . "){?>";
149 $this->openTag($compiler, 'elseif', array($nesting, $compiler->tag_nocache));
150 return "<?php }elseif({$parameter['if condition']}){?>";
154 foreach ($compiler->prefix_code as $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'] . "){?>";
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'] . "){?>";
168 return "<?php }else{?>{$tmp}<?php if ({$parameter['if condition']}){?>";
176 * Smarty Internal Plugin Compile Ifclose Class
179 * @subpackage Compiler
181 class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase {
184 * Compiles code for the {/if} tag
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
191 public function compile($args, $compiler, $parameter)
193 // must endblock be nocache?
194 if ($compiler->nocache) {
195 $compiler->tag_nocache = true;
197 list($nesting, $compiler->nocache) = $this->closeTag($compiler, array('if', 'else', 'elseif'));
199 for ($i = 0; $i < $nesting; $i++) {
202 return "<?php {$tmp}?>";