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 */ 17class 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 \Smarty_Internal_TemplateCompilerBase $compiler compiler object 24 * @param array $parameter array with compilation parameter 25 * 26 * @return string compiled code 27 * @throws \SmartyCompilerException 28 */ 29 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) 30 { 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; 36 37 if (!array_key_exists("if condition", $parameter)) { 38 $compiler->trigger_template_error("missing if condition", null, true); 39 } 40 41 if (is_array($parameter[ 'if condition' ])) { 42 if (is_array($parameter[ 'if condition' ][ 'var' ])) { 43 $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; 44 } else { 45 $var = $parameter[ 'if condition' ][ 'var' ]; 46 } 47 if ($compiler->nocache) { 48 // create nocache var to make it know for further compiling 49 $compiler->setNocacheInVariable($var); 50 } 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' ])); 60 } else { 61 $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ]; 62 $_output .= $assignCompiler->compile($assignAttr, $compiler, array()); 63 } 64 $_output .= "<?php if ({$prefixVar}) {?>"; 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 */ 78class 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 \Smarty_Internal_TemplateCompilerBase $compiler compiler object 85 * @param array $parameter array with compilation parameter 86 * 87 * @return string compiled code 88 */ 89 public function compile($args, Smarty_Internal_TemplateCompilerBase $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 */ 104class 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 \Smarty_Internal_TemplateCompilerBase $compiler compiler object 111 * @param array $parameter array with compilation parameter 112 * 113 * @return string compiled code 114 * @throws \SmartyCompilerException 115 */ 116 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) 117 { 118 // check and get attributes 119 $_attr = $this->getAttributes($compiler, $args); 120 121 list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif')); 122 123 if (!array_key_exists("if condition", $parameter)) { 124 $compiler->trigger_template_error("missing elseif condition", null, true); 125 } 126 127 $assignCode = ''; 128 $var = ''; 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' ]; 133 } else { 134 $var = $parameter[ 'if condition' ][ 'var' ]; 135 } 136 if ($compiler->nocache) { 137 // create nocache var to make it know for further compiling 138 $compiler->setNocacheInVariable($var); 139 } 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' ])); 149 } else { 150 $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ]; 151 $assignCode .= $assignCompiler->compile($assignAttr, $compiler, array()); 152 } 153 } else { 154 $condition_by_assign = false; 155 } 156 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}) {?>"); 163 } else { 164 $this->openTag($compiler, 'elseif', array($nesting, $compiler->tag_nocache)); 165 return "<?php } elseif ({$parameter['if condition']}) {?>"; 166 } 167 } else { 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}) {?>"); 173 } else { 174 return $compiler->appendCode($_output, "<?php if ({$parameter['if condition']}) {?>"); 175 } 176 } 177 } 178} 179 180/** 181 * Smarty Internal Plugin Compile Ifclose Class 182 * 183 * @package Smarty 184 * @subpackage Compiler 185 */ 186class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase 187{ 188 /** 189 * Compiles code for the {/if} tag 190 * 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 194 * 195 * @return string compiled code 196 */ 197 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) 198 { 199 // must endblock be nocache? 200 if ($compiler->nocache) { 201 $compiler->tag_nocache = true; 202 } 203 list($nesting, $compiler->nocache) = $this->closeTag($compiler, array('if', 'else', 'elseif')); 204 $tmp = ''; 205 for ($i = 0; $i < $nesting; $i ++) { 206 $tmp .= '}'; 207 } 208 209 return "<?php {$tmp}?>"; 210 } 211} 212