3 * Smarty Internal Plugin Compile If
5 * Compiles the {if} {else} {elseif} {/if} tags
13 * Smarty Internal Plugin Compile If Class
15 class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase {
17 * Compiles code for the {if} tag
19 * @param array $args array with attributes from parser
20 * @param object $compiler compiler object
21 * @param array $parameter array with compilation parameter
22 * @return string compiled code
24 public function compile($args, $compiler, $parameter)
26 $this->compiler = $compiler;
27 // check and get attributes
28 $_attr = $this->_get_attributes($args);
29 $this->_open_tag('if',array(1,$this->compiler->nocache));
30 // must whole block be nocache ?
31 $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
32 if (is_array($parameter['if condition'])) {
33 if ($this->compiler->nocache) {
35 // create nocache var to make it know for further compiling
36 if (is_array($parameter['if condition']['var'])) {
37 $this->compiler->template->tpl_vars[trim($parameter['if condition']['var']['var'], "'")] = new Smarty_variable(null, true);
39 $this->compiler->template->tpl_vars[trim($parameter['if condition']['var'], "'")] = new Smarty_variable(null, true);
44 if (is_array($parameter['if condition']['var'])) {
45 $_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";
46 $_output .= "if (\$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']['var']."]->value".$parameter['if condition']['var']['smarty_internal_index']." = ".$parameter['if condition']['value']."){?>";
48 $_output = "<?php \$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']."] = new Smarty_Variable(\$_smarty_tpl->getVariable(".$parameter['if condition']['var'].",null,true,false)->value{$_nocache});";
49 $_output .= "if (\$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']."]->value = ".$parameter['if condition']['value']."){?>";
53 return "<?php if ({$parameter['if condition']}){?>";
59 * Smarty Internal Plugin Compile Else Class
61 class Smarty_Internal_Compile_Else extends Smarty_Internal_CompileBase {
63 * Compiles code for the {else} tag
65 * @param array $args array with attributes from parser
66 * @param object $compiler compiler object
67 * @param array $parameter array with compilation parameter
68 * @return string compiled code
70 public function compile($args, $compiler, $parameter)
72 $this->compiler = $compiler;
73 list($nesting, $compiler->tag_nocache) = $this->_close_tag(array('if', 'elseif'));
74 $this->_open_tag('else',array($nesting,$compiler->tag_nocache));
76 return "<?php }else{ ?>";
81 * Smarty Internal Plugin Compile ElseIf Class
83 class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase {
85 * Compiles code for the {elseif} tag
87 * @param array $args array with attributes from parser
88 * @param object $compiler compiler object
89 * @param array $parameter array with compilation parameter
90 * @return string compiled code
92 public function compile($args, $compiler, $parameter)
94 $this->compiler = $compiler;
95 // check and get attributes
96 $_attr = $this->_get_attributes($args);
98 list($nesting, $compiler->tag_nocache) = $this->_close_tag(array('if', 'elseif'));
100 if (is_array($parameter['if condition'])) {
101 $condition_by_assign = true;
102 if ($this->compiler->nocache) {
104 // create nocache var to make it know for further compiling
105 if (is_array($parameter['if condition']['var'])) {
106 $this->compiler->template->tpl_vars[trim($parameter['if condition']['var']['var'], "'")] = new Smarty_variable(null, true);
108 $this->compiler->template->tpl_vars[trim($parameter['if condition']['var'], "'")] = new Smarty_variable(null, true);
114 $condition_by_assign = false;
117 if (empty($this->compiler->prefix_code)) {
118 if ($condition_by_assign) {
119 $this->_open_tag('elseif', array($nesting + 1, $compiler->tag_nocache));
120 if (is_array($parameter['if condition']['var'])) {
121 $_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";
122 $_output .= "if (\$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']['var']."]->value".$parameter['if condition']['var']['smarty_internal_index']." = ".$parameter['if condition']['value']."){?>";
124 $_output = "<?php }else{ \$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']."] = new Smarty_Variable(\$_smarty_tpl->getVariable(".$parameter['if condition']['var'].",null,true,false)->value{$_nocache});";
125 $_output .= "if (\$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']."]->value = ".$parameter['if condition']['value']."){?>";
129 $this->_open_tag('elseif', array($nesting, $compiler->tag_nocache));
130 return "<?php }elseif({$parameter['if condition']}){?>";
134 foreach ($this->compiler->prefix_code as $code) $tmp .= $code;
135 $this->compiler->prefix_code = array();
136 $this->_open_tag('elseif', array($nesting + 1, $compiler->tag_nocache));
137 if ($condition_by_assign) {
138 if (is_array($parameter['if condition']['var'])) {
139 $_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";
140 $_output .= "if (\$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']['var']."]->value".$parameter['if condition']['var']['smarty_internal_index']." = ".$parameter['if condition']['value']."){?>";
142 $_output = "<?php }else{?>{$tmp}<?php \$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']."] = new Smarty_Variable(\$_smarty_tpl->getVariable(".$parameter['if condition']['var'].",null,true,false)->value{$_nocache});";
143 $_output .= "if (\$_smarty_tpl->tpl_vars[".$parameter['if condition']['var']."]->value = ".$parameter['if condition']['value']."){?>";
147 return "<?php }else{?>{$tmp}<?php if ({$parameter['if condition']}){?>";
154 * Smarty Internal Plugin Compile Ifclose Class
156 class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase {
158 * Compiles code for the {/if} tag
160 * @param array $args array with attributes from parser
161 * @param object $compiler compiler object
162 * @param array $parameter array with compilation parameter
163 * @return string compiled code
165 public function compile($args, $compiler, $parameter)
167 $this->compiler = $compiler;
168 // must endblock be nocache?
169 if ($this->compiler->nocache) {
170 $this->compiler->tag_nocache = true;
172 list($nesting, $this->compiler->nocache) = $this->_close_tag(array('if', 'else', 'elseif'));
174 for ($i = 0; $i < $nesting ; $i++) $tmp .= '}';
175 return "<?php {$tmp}?>";