PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Associates/Smarty/sysplugins/smarty_internal_compile_if.php

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