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 11KB

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