smarty_internal_compile_if.php 9.0 KB

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