PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/application/library/Thirdpart/Smarty/libs/sysplugins/smarty_internal_compile_if.php

https://gitlab.com/flyhope/Hiblog
PHP | 221 lines | 131 code | 16 blank | 74 comment | 46 complexity | 67820a4db522a826c4417356b834928f MD5 | raw file
  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 object $compiler compiler object
  23. * @param array $parameter array with compilation parameter
  24. *
  25. * @return string compiled code
  26. */
  27. public function compile($args, $compiler, $parameter)
  28. {
  29. // check and get attributes
  30. $_attr = $this->getAttributes($compiler, $args);
  31. $this->openTag($compiler, 'if', array(1, $compiler->nocache));
  32. // must whole block be nocache ?
  33. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  34. if (!array_key_exists("if condition", $parameter)) {
  35. $compiler->trigger_template_error("missing if condition", $compiler->lex->taglineno);
  36. }
  37. if (is_array($parameter['if condition'])) {
  38. if ($compiler->nocache) {
  39. $_nocache = ',true';
  40. // create nocache var to make it know for further compiling
  41. if (is_array($parameter['if condition']['var'])) {
  42. $var = trim($parameter['if condition']['var']['var'], "'");
  43. } else {
  44. $var = trim($parameter['if condition']['var'], "'");
  45. }
  46. if (isset($compiler->template->tpl_vars[$var])) {
  47. $compiler->template->tpl_vars[$var]->nocache = true;
  48. } else {
  49. $compiler->template->tpl_vars[$var] = new Smarty_Variable(null, true);
  50. }
  51. } else {
  52. $_nocache = '';
  53. }
  54. if (is_array($parameter['if condition']['var'])) {
  55. $_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";
  56. $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['value'] . ") {?>";
  57. } else {
  58. $_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});";
  59. $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . ") {?>";
  60. }
  61. return $_output;
  62. } else {
  63. return "<?php if ({$parameter['if condition']}) {?>";
  64. }
  65. }
  66. }
  67. /**
  68. * Smarty Internal Plugin Compile Else Class
  69. *
  70. * @package Smarty
  71. * @subpackage Compiler
  72. */
  73. class Smarty_Internal_Compile_Else extends Smarty_Internal_CompileBase
  74. {
  75. /**
  76. * Compiles code for the {else} tag
  77. *
  78. * @param array $args array with attributes from parser
  79. * @param object $compiler compiler object
  80. * @param array $parameter array with compilation parameter
  81. *
  82. * @return string compiled code
  83. */
  84. public function compile($args, $compiler, $parameter)
  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 object $compiler compiler object
  104. * @param array $parameter array with compilation parameter
  105. *
  106. * @return string compiled code
  107. */
  108. public function compile($args, $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", $compiler->lex->taglineno);
  115. }
  116. if (is_array($parameter['if condition'])) {
  117. $condition_by_assign = true;
  118. if ($compiler->nocache) {
  119. $_nocache = ',true';
  120. // create nocache var to make it know for further compiling
  121. if (is_array($parameter['if condition']['var'])) {
  122. $var = trim($parameter['if condition']['var']['var'], "'");
  123. } else {
  124. $var = trim($parameter['if condition']['var'], "'");
  125. }
  126. if (isset($compiler->template->tpl_vars[$var])) {
  127. $compiler->template->tpl_vars[$var]->nocache = true;
  128. } else {
  129. $compiler->template->tpl_vars[$var] = new Smarty_Variable(null, true);
  130. }
  131. } else {
  132. $_nocache = '';
  133. }
  134. } else {
  135. $condition_by_assign = false;
  136. }
  137. if (empty($compiler->prefix_code)) {
  138. if ($condition_by_assign) {
  139. $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
  140. if (is_array($parameter['if condition']['var'])) {
  141. $_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";
  142. $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['value'] . ") {?>";
  143. } else {
  144. $_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});";
  145. $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . ") {?>";
  146. }
  147. return $_output;
  148. } else {
  149. $this->openTag($compiler, 'elseif', array($nesting, $compiler->tag_nocache));
  150. return "<?php } elseif ({$parameter['if condition']}) {?>";
  151. }
  152. } else {
  153. $tmp = '';
  154. foreach ($compiler->prefix_code as $code) {
  155. $tmp = $compiler->appendCode($tmp, $code);
  156. }
  157. $compiler->prefix_code = array();
  158. $tmp = $compiler->appendCode("<?php } else {?>", $tmp);
  159. $this->openTag($compiler, 'elseif', array($nesting + 1, $compiler->tag_nocache));
  160. if ($condition_by_assign) {
  161. if (is_array($parameter['if condition']['var'])) {
  162. $_output = $compiler->appendCode($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");
  163. $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['value'] . ") {?>";
  164. } else {
  165. $_output = $compiler->appendCode($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});");
  166. $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " . $parameter['if condition']['value'] . ") {?>";
  167. }
  168. return $_output;
  169. } else {
  170. return $compiler->appendCode($tmp, "<?php if ({$parameter['if condition']}) {?>");
  171. }
  172. }
  173. }
  174. }
  175. /**
  176. * Smarty Internal Plugin Compile Ifclose Class
  177. *
  178. * @package Smarty
  179. * @subpackage Compiler
  180. */
  181. class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase
  182. {
  183. /**
  184. * Compiles code for the {/if} tag
  185. *
  186. * @param array $args array with attributes from parser
  187. * @param object $compiler compiler object
  188. * @param array $parameter array with compilation parameter
  189. *
  190. * @return string compiled code
  191. */
  192. public function compile($args, $compiler, $parameter)
  193. {
  194. // must endblock be nocache?
  195. if ($compiler->nocache) {
  196. $compiler->tag_nocache = true;
  197. }
  198. list($nesting, $compiler->nocache) = $this->closeTag($compiler, array('if', 'else', 'elseif'));
  199. $tmp = '';
  200. for ($i = 0; $i < $nesting; $i ++) {
  201. $tmp .= '}';
  202. }
  203. return "<?php {$tmp}?>";
  204. }
  205. }