smarty_internal_compile_section.php 7.3 KB

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