smarty_internal_compile_section.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Section
  4. * Compiles the {section} {sectionelse} {/section} tags
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Section Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Section extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('name', 'loop');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('name', 'loop');
  32. /**
  33. * Attribute definition: Overwrites base class.
  34. *
  35. * @var array
  36. * @see Smarty_Internal_CompileBase
  37. */
  38. public $optional_attributes = array('start', 'step', 'max', 'show');
  39. /**
  40. * Compiles code for the {section} tag
  41. *
  42. * @param array $args array with attributes from parser
  43. * @param object $compiler compiler object
  44. *
  45. * @return string compiled code
  46. */
  47. public function compile($args, $compiler)
  48. {
  49. // check and get attributes
  50. $_attr = $this->getAttributes($compiler, $args);
  51. $this->openTag($compiler, 'section', array('section', $compiler->nocache));
  52. // maybe nocache because of nocache variables
  53. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  54. $output = "<?php ";
  55. $section_name = $_attr['name'];
  56. $output .= "if (isset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name])) unset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]);\n";
  57. $section_props = "\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]";
  58. foreach ($_attr as $attr_name => $attr_value) {
  59. switch ($attr_name) {
  60. case 'loop':
  61. $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int) \$_loop); unset(\$_loop);\n";
  62. break;
  63. case 'show':
  64. if (is_bool($attr_value)) {
  65. $show_attr_value = $attr_value ? 'true' : 'false';
  66. } else {
  67. $show_attr_value = "(bool) $attr_value";
  68. }
  69. $output .= "{$section_props}['show'] = $show_attr_value;\n";
  70. break;
  71. case 'name':
  72. $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
  73. break;
  74. case 'max':
  75. case 'start':
  76. $output .= "{$section_props}['$attr_name'] = (int) $attr_value;\n";
  77. break;
  78. case 'step':
  79. $output .= "{$section_props}['$attr_name'] = ((int) $attr_value) == 0 ? 1 : (int) $attr_value;\n";
  80. break;
  81. }
  82. }
  83. if (!isset($_attr['show'])) {
  84. $output .= "{$section_props}['show'] = true;\n";
  85. }
  86. if (!isset($_attr['loop'])) {
  87. $output .= "{$section_props}['loop'] = 1;\n";
  88. }
  89. if (!isset($_attr['max'])) {
  90. $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
  91. } else {
  92. $output .= "if ({$section_props}['max'] < 0)\n" . " {$section_props}['max'] = {$section_props}['loop'];\n";
  93. }
  94. if (!isset($_attr['step'])) {
  95. $output .= "{$section_props}['step'] = 1;\n";
  96. }
  97. if (!isset($_attr['start'])) {
  98. $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
  99. } else {
  100. $output .= "if ({$section_props}['start'] < 0)\n" . " {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" . "else\n" . " {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
  101. }
  102. $output .= "if ({$section_props}['show']) {\n";
  103. if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) {
  104. $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
  105. } else {
  106. $output .= " {$section_props}['total'] = min(ceil(({$section_props}['step'] > 0 ? {$section_props}['loop'] - {$section_props}['start'] : {$section_props}['start']+1)/abs({$section_props}['step'])), {$section_props}['max']);\n";
  107. }
  108. $output .= " if ({$section_props}['total'] == 0)\n" . " {$section_props}['show'] = false;\n" . "} else\n" . " {$section_props}['total'] = 0;\n";
  109. $output .= "if ({$section_props}['show']):\n";
  110. $output .= "
  111. for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
  112. {$section_props}['iteration'] <= {$section_props}['total'];
  113. {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
  114. $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
  115. $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
  116. $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
  117. $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
  118. $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
  119. $output .= "?>";
  120. return $output;
  121. }
  122. }
  123. /**
  124. * Smarty Internal Plugin Compile Sectionelse Class
  125. *
  126. * @package Smarty
  127. * @subpackage Compiler
  128. */
  129. class Smarty_Internal_Compile_Sectionelse extends Smarty_Internal_CompileBase
  130. {
  131. /**
  132. * Compiles code for the {sectionelse} tag
  133. *
  134. * @param array $args array with attributes from parser
  135. * @param object $compiler compiler object
  136. *
  137. * @return string compiled code
  138. */
  139. public function compile($args, $compiler)
  140. {
  141. // check and get attributes
  142. $_attr = $this->getAttributes($compiler, $args);
  143. list($openTag, $nocache) = $this->closeTag($compiler, array('section'));
  144. $this->openTag($compiler, 'sectionelse', array('sectionelse', $nocache));
  145. return "<?php endfor; else: ?>";
  146. }
  147. }
  148. /**
  149. * Smarty Internal Plugin Compile Sectionclose Class
  150. *
  151. * @package Smarty
  152. * @subpackage Compiler
  153. */
  154. class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase
  155. {
  156. /**
  157. * Compiles code for the {/section} tag
  158. *
  159. * @param array $args array with attributes from parser
  160. * @param object $compiler compiler object
  161. *
  162. * @return string compiled code
  163. */
  164. public function compile($args, $compiler)
  165. {
  166. // check and get attributes
  167. $_attr = $this->getAttributes($compiler, $args);
  168. // must endblock be nocache?
  169. if ($compiler->nocache) {
  170. $compiler->tag_nocache = true;
  171. }
  172. list($openTag, $compiler->nocache) = $this->closeTag($compiler, array('section', 'sectionelse'));
  173. if ($openTag == 'sectionelse') {
  174. return "<?php endif; ?>";
  175. } else {
  176. return "<?php endfor; endif; ?>";
  177. }
  178. }
  179. }