smarty_internal_compile_section.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. class Smarty_Internal_Compile_Section extends Smarty_Internal_CompileBase {
  15. /**
  16. * Compiles code for the {section} 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('name', 'loop');
  26. $this->optional_attributes = array('start', 'step', 'max', 'show');
  27. // check and get attributes
  28. $_attr = $this->_get_attributes($args);
  29. $this->_open_tag('section', array('section',$this->compiler->nocache));
  30. // maybe nocache because of nocache variables
  31. $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
  32. $output = "<?php ";
  33. $section_name = $_attr['name'];
  34. $output .= "unset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]);\n";
  35. $section_props = "\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]";
  36. foreach ($_attr as $attr_name => $attr_value) {
  37. switch ($attr_name) {
  38. case 'loop':
  39. $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";
  40. break;
  41. case 'show':
  42. if (is_bool($attr_value))
  43. $show_attr_value = $attr_value ? 'true' : 'false';
  44. else
  45. $show_attr_value = "(bool)$attr_value";
  46. $output .= "{$section_props}['show'] = $show_attr_value;\n";
  47. break;
  48. case 'name':
  49. $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
  50. break;
  51. case 'max':
  52. case 'start':
  53. $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
  54. break;
  55. case 'step':
  56. $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
  57. break;
  58. }
  59. }
  60. if (!isset($_attr['show']))
  61. $output .= "{$section_props}['show'] = true;\n";
  62. if (!isset($_attr['loop']))
  63. $output .= "{$section_props}['loop'] = 1;\n";
  64. if (!isset($_attr['max']))
  65. $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
  66. else
  67. $output .= "if ({$section_props}['max'] < 0)\n" . " {$section_props}['max'] = {$section_props}['loop'];\n";
  68. if (!isset($_attr['step']))
  69. $output .= "{$section_props}['step'] = 1;\n";
  70. if (!isset($_attr['start']))
  71. $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
  72. else {
  73. $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";
  74. }
  75. $output .= "if ({$section_props}['show']) {\n";
  76. if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) {
  77. $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
  78. } else {
  79. $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";
  80. }
  81. $output .= " if ({$section_props}['total'] == 0)\n" . " {$section_props}['show'] = false;\n" . "} else\n" . " {$section_props}['total'] = 0;\n";
  82. $output .= "if ({$section_props}['show']):\n";
  83. $output .= "
  84. for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
  85. {$section_props}['iteration'] <= {$section_props}['total'];
  86. {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
  87. $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
  88. $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
  89. $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
  90. $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
  91. $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
  92. $output .= "?>";
  93. return $output;
  94. }
  95. }
  96. /**
  97. * Smarty Internal Plugin Compile Sectionelse Class
  98. */
  99. class Smarty_Internal_Compile_Sectionelse extends Smarty_Internal_CompileBase {
  100. /**
  101. * Compiles code for the {sectionelse} tag
  102. *
  103. * @param array $args array with attributes from parser
  104. * @param object $compiler compiler object
  105. * @return string compiled code
  106. */
  107. public function compile($args, $compiler)
  108. {
  109. $this->compiler = $compiler;
  110. // check and get attributes
  111. $_attr = $this->_get_attributes($args);
  112. list($_open_tag, $nocache) = $this->_close_tag(array('section'));
  113. $this->_open_tag('sectionelse',array('sectionelse', $nocache));
  114. return "<?php endfor; else: ?>";
  115. }
  116. }
  117. /**
  118. * Smarty Internal Plugin Compile Sectionclose Class
  119. */
  120. class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase {
  121. /**
  122. * Compiles code for the {/section} tag
  123. *
  124. * @param array $args array with attributes from parser
  125. * @param object $compiler compiler object
  126. * @return string compiled code
  127. */
  128. public function compile($args, $compiler)
  129. {
  130. $this->compiler = $compiler;
  131. // check and get attributes
  132. $_attr = $this->_get_attributes($args);
  133. // must endblock be nocache?
  134. if ($this->compiler->nocache) {
  135. $this->compiler->tag_nocache = true;
  136. }
  137. list($_open_tag, $this->compiler->nocache) = $this->_close_tag(array('section', 'sectionelse'));
  138. if ($_open_tag == 'sectionelse')
  139. return "<?php endif; ?>";
  140. else
  141. return "<?php endfor; endif; ?>";
  142. }
  143. }
  144. ?>