smarty_internal_compile_if.php 8.3 KB

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