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