smarty_internal_compile_if.php 8.5 KB

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