You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_compile_if.php 8.1KB

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