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 object $compiler compiler object
24 * @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'] . ") {?>";
62 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
82 * @return string compiled code
84 public function compile($args, $compiler, $parameter)
86 list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
87 $this->openTag($compiler, 'else', array($nesting, $compiler->tag_nocache));
89 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
108 * @return string compiled code
110 public function compile($args, $compiler, $parameter)
112 // check and get attributes
113 $_attr = $this->getAttributes($compiler, $args);
115 list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif'));
117 if (!array_key_exists("if condition", $parameter)) {
118 $compiler->trigger_template_error("missing elseif condition", $compiler->lex->taglineno);
121 if (is_array($parameter['if condition'])) {
122 $condition_by_assign = true;
123 if ($compiler->nocache) {
125 // create nocache var to make it know for further compiling
126 if (is_array($parameter['if condition']['var'])) {
127 $compiler->template->tpl_vars[trim($parameter['if condition']['var']['var'], "'")] = new Smarty_variable(null, true);
129 $compiler->template->tpl_vars[trim($parameter['if condition']['var'], "'")] = new Smarty_variable(null, true);
135 $condition_by_assign = false;
138 if (empty($compiler->prefix_code)) {
139 if ($condition_by_assign) {
140 $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
141 if (is_array($parameter['if condition']['var'])) {
142 $_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";
143 $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['value'] . ") {?>";
145 $_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});";
146 $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . ") {?>";
151 $this->openTag($compiler, 'elseif', array($nesting, $compiler->tag_nocache));
153 return "<?php } elseif ({$parameter['if condition']}) {?>";
157 foreach ($compiler->prefix_code as $code) {
160 $compiler->prefix_code = array();
161 $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
162 if ($condition_by_assign) {
163 if (is_array($parameter['if condition']['var'])) {
164 $_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";
165 $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['value'] . ") {?>";
167 $_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});";
168 $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . ") {?>";
173 return "<?php } else {?>{$tmp}<?php if ({$parameter['if condition']}) {?>";
180 * Smarty Internal Plugin Compile Ifclose Class
183 * @subpackage Compiler
185 class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase
188 * Compiles code for the {/if} tag
190 * @param array $args array with attributes from parser
191 * @param object $compiler compiler object
192 * @param array $parameter array with compilation parameter
194 * @return string compiled code
196 public function compile($args, $compiler, $parameter)
198 // must endblock be nocache?
199 if ($compiler->nocache) {
200 $compiler->tag_nocache = true;
202 list($nesting, $compiler->nocache) = $this->closeTag($compiler, array('if', 'else', 'elseif'));
204 for ($i = 0; $i < $nesting; $i ++) {
208 return "<?php {$tmp}?>";