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 if (!isset($parameter[ 'if condition' ])) { 37 $compiler->trigger_template_error('missing if condition', null, true); 38 } 39 if (is_array($parameter[ 'if condition' ])) { 40 if (is_array($parameter[ 'if condition' ][ 'var' ])) { 41 $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; 42 } else { 43 $var = $parameter[ 'if condition' ][ 'var' ]; 44 } 45 if ($compiler->nocache) { 46 // create nocache var to make it know for further compiling 47 $compiler->setNocacheInVariable($var); 48 } 49 $prefixVar = $compiler->getNewPrefixVariable(); 50 $_output = "<?php {$prefixVar} = {$parameter[ 'if condition' ][ 'value' ]};?>\n"; 51 $assignAttr = array(); 52 $assignAttr[][ 'value' ] = $prefixVar; 53 $assignCompiler = new Smarty_Internal_Compile_Assign(); 54 if (is_array($parameter[ 'if condition' ][ 'var' ])) { 55 $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; 56 $_output .= $assignCompiler->compile( 57 $assignAttr, 58 $compiler, 59 array('smarty_internal_index' => $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ]) 60 ); 61 } else { 62 $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ]; 63 $_output .= $assignCompiler->compile($assignAttr, $compiler, array()); 64 } 65 $_output .= "<?php if ({$prefixVar}) {?>"; 66 return $_output; 67 } else { 68 return "<?php if ({$parameter['if condition']}) {?>"; 69 } 70 } 71} 72 73/** 74 * Smarty Internal Plugin Compile Else Class 75 * 76 * @package Smarty 77 * @subpackage Compiler 78 */ 79class Smarty_Internal_Compile_Else extends Smarty_Internal_CompileBase 80{ 81 /** 82 * Compiles code for the {else} tag 83 * 84 * @param array $args array with attributes from parser 85 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object 86 * 87 * @return string compiled code 88 */ 89 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler) 90 { 91 list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif')); 92 $this->openTag($compiler, 'else', array($nesting, $compiler->tag_nocache)); 93 return '<?php } else { ?>'; 94 } 95} 96 97/** 98 * Smarty Internal Plugin Compile ElseIf Class 99 * 100 * @package Smarty 101 * @subpackage Compiler 102 */ 103class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase 104{ 105 /** 106 * Compiles code for the {elseif} tag 107 * 108 * @param array $args array with attributes from parser 109 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object 110 * @param array $parameter array with compilation parameter 111 * 112 * @return string compiled code 113 * @throws \SmartyCompilerException 114 */ 115 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) 116 { 117 // check and get attributes 118 $_attr = $this->getAttributes($compiler, $args); 119 list($nesting, $compiler->tag_nocache) = $this->closeTag($compiler, array('if', 'elseif')); 120 if (!isset($parameter[ 'if condition' ])) { 121 $compiler->trigger_template_error('missing elseif condition', null, true); 122 } 123 $assignCode = ''; 124 $var = ''; 125 if (is_array($parameter[ 'if condition' ])) { 126 $condition_by_assign = true; 127 if (is_array($parameter[ 'if condition' ][ 'var' ])) { 128 $var = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; 129 } else { 130 $var = $parameter[ 'if condition' ][ 'var' ]; 131 } 132 if ($compiler->nocache) { 133 // create nocache var to make it know for further compiling 134 $compiler->setNocacheInVariable($var); 135 } 136 $prefixVar = $compiler->getNewPrefixVariable(); 137 $assignCode = "<?php {$prefixVar} = {$parameter[ 'if condition' ][ 'value' ]};?>\n"; 138 $assignCompiler = new Smarty_Internal_Compile_Assign(); 139 $assignAttr = array(); 140 $assignAttr[][ 'value' ] = $prefixVar; 141 if (is_array($parameter[ 'if condition' ][ 'var' ])) { 142 $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ][ 'var' ]; 143 $assignCode .= $assignCompiler->compile( 144 $assignAttr, 145 $compiler, 146 array('smarty_internal_index' => $parameter[ 'if condition' ][ 'var' ][ 'smarty_internal_index' ]) 147 ); 148 } else { 149 $assignAttr[][ 'var' ] = $parameter[ 'if condition' ][ 'var' ]; 150 $assignCode .= $assignCompiler->compile($assignAttr, $compiler, array()); 151 } 152 } else { 153 $condition_by_assign = false; 154 } 155 $prefixCode = $compiler->getPrefixCode(); 156 if (empty($prefixCode)) { 157 if ($condition_by_assign) { 158 $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache)); 159 $_output = $compiler->appendCode("<?php } else {\n?>", $assignCode); 160 return $compiler->appendCode($_output, "<?php if ({$prefixVar}) {?>"); 161 } else { 162 $this->openTag($compiler, 'elseif', array($nesting, $compiler->tag_nocache)); 163 return "<?php } elseif ({$parameter['if condition']}) {?>"; 164 } 165 } else { 166 $_output = $compiler->appendCode("<?php } else {\n?>", $prefixCode); 167 $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache)); 168 if ($condition_by_assign) { 169 $_output = $compiler->appendCode($_output, $assignCode); 170 return $compiler->appendCode($_output, "<?php if ({$prefixVar}) {?>"); 171 } else { 172 return $compiler->appendCode($_output, "<?php if ({$parameter['if condition']}) {?>"); 173 } 174 } 175 } 176} 177 178/** 179 * Smarty Internal Plugin Compile Ifclose Class 180 * 181 * @package Smarty 182 * @subpackage Compiler 183 */ 184class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase 185{ 186 /** 187 * Compiles code for the {/if} tag 188 * 189 * @param array $args array with attributes from parser 190 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object 191 * 192 * @return string compiled code 193 */ 194 public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler) 195 { 196 // must endblock be nocache? 197 if ($compiler->nocache) { 198 $compiler->tag_nocache = true; 199 } 200 list($nesting, $compiler->nocache) = $this->closeTag($compiler, array('if', 'else', 'elseif')); 201 $tmp = ''; 202 for ($i = 0; $i < $nesting; $i++) { 203 $tmp .= '}'; 204 } 205 return "<?php {$tmp}?>"; 206 } 207} 208