smarty_internal_compile_if.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase {
  15. /**
  16. * Compiles code for the {if} tag
  17. *
  18. * @param array $args array with attributes from parser
  19. * @param object $compiler compiler object
  20. * @return string compiled code
  21. */
  22. public function compile($args, $compiler)
  23. {
  24. $this->compiler = $compiler;
  25. $this->required_attributes = array('if condition');
  26. // check and get attributes
  27. $_attr = $this->_get_attributes($args);
  28. $this->_open_tag('if',array(1,$compiler->tag_nocache));
  29. if (is_array($args['if condition'])) {
  30. $_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[".$args['if condition']['var']."])) \$_smarty_tpl->tpl_vars[".$args['if condition']['var']."] = new Smarty_Variable;";
  31. $_output .= "if (\$_smarty_tpl->tpl_vars[".$args['if condition']['var']."]->value = ".$args['if condition']['value']."){?>";
  32. return $_output;
  33. } else {
  34. return "<?php if ({$args['if condition']}){?>";
  35. }
  36. }
  37. }
  38. /**
  39. * Smarty Internal Plugin Compile Else Class
  40. */
  41. class Smarty_Internal_Compile_Else extends Smarty_Internal_CompileBase {
  42. /**
  43. * Compiles code for the {else} tag
  44. *
  45. * @param array $args array with attributes from parser
  46. * @param object $compiler compiler object
  47. * @return string compiled code
  48. */
  49. public function compile($args, $compiler)
  50. {
  51. $this->compiler = $compiler;
  52. list($nesting, $compiler->tag_nocache) = $this->_close_tag(array('if', 'elseif'));
  53. $this->_open_tag('else',array($nesting,$compiler->tag_nocache));
  54. return "<?php }else{ ?>";
  55. }
  56. }
  57. /**
  58. * Smarty Internal Plugin Compile ElseIf Class
  59. */
  60. class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase {
  61. /**
  62. * Compiles code for the {elseif} tag
  63. *
  64. * @param array $args array with attributes from parser
  65. * @param object $compiler compiler object
  66. * @return string compiled code
  67. */
  68. public function compile($args, $compiler)
  69. {
  70. $this->compiler = $compiler;
  71. $this->required_attributes = array('if condition');
  72. // check and get attributes
  73. $_attr = $this->_get_attributes($args);
  74. list($nesting, $compiler->tag_nocache) = $this->_close_tag(array('if', 'elseif'));
  75. if (empty($this->compiler->prefix_code)) {
  76. $this->_open_tag('elseif', array($nesting, $compiler->tag_nocache));
  77. return "<?php }elseif({$args['if condition']}){?>";
  78. } else {
  79. $tmp = '';
  80. foreach ($this->compiler->prefix_code as $code) $tmp .= $code;
  81. $this->compiler->prefix_code = array();
  82. $this->_open_tag('elseif', array($nesting + 1, $compiler->tag_nocache));
  83. return "<?php }else{?>{$tmp}<?php if ({$args['if condition']}){?>";
  84. }
  85. }
  86. }
  87. /**
  88. * Smarty Internal Plugin Compile Ifclose Class
  89. */
  90. class Smarty_Internal_Compile_Ifclose extends Smarty_Internal_CompileBase {
  91. /**
  92. * Compiles code for the {/if} tag
  93. *
  94. * @param array $args array with attributes from parser
  95. * @param object $compiler compiler object
  96. * @return string compiled code
  97. */
  98. public function compile($args, $compiler)
  99. {
  100. $this->compiler = $compiler;
  101. list($nesting, $compiler->tag_nocache) = $this->_close_tag(array('if', 'else', 'elseif'));
  102. $tmp = '';
  103. for ($i = 0; $i < $nesting ; $i++) $tmp .= '}';
  104. return "<?php {$tmp}?>";
  105. }
  106. }
  107. ?>