compile.section_start.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Template Lite section_start compile plugin converted from Smarty
  4. *
  5. * Type: compile
  6. * Name: section_start
  7. */
  8. function compile_section_start($arguments, &$object)
  9. {
  10. $attrs = $object->_parse_arguments($arguments);
  11. $arg_list = array();
  12. $output = '<?php ';
  13. $section_name = $attrs['name'];
  14. if (empty($section_name))
  15. {
  16. $object->trigger_error("missing section name", E_USER_ERROR, __FILE__, __LINE__);
  17. }
  18. $output .= "if (isset(\$this->_sections['$section_name'])) unset(\$this->_sections['$section_name']);\n";
  19. $section_props = "\$this->_sections['$section_name']";
  20. foreach ($attrs as $attr_name => $attr_value)
  21. {
  22. switch ($attr_name)
  23. {
  24. case 'loop':
  25. $output .= "{$section_props}['loop'] = is_array($attr_value) ? count($attr_value) : max(0, (int)$attr_value);\n";
  26. break;
  27. case 'show':
  28. if (is_bool($attr_value))
  29. {
  30. $show_attr_value = $attr_value ? 'true' : 'false';
  31. }
  32. else
  33. {
  34. $show_attr_value = "(bool)$attr_value";
  35. }
  36. $output .= "{$section_props}['show'] = $show_attr_value;\n";
  37. break;
  38. case 'name':
  39. $output .= "{$section_props}['$attr_name'] = '$attr_value';\n";
  40. break;
  41. case 'max':
  42. case 'start':
  43. $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
  44. break;
  45. case 'step':
  46. $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
  47. break;
  48. default:
  49. $object->trigger_error("unknown section attribute - '$attr_name'", E_USER_ERROR, __FILE__, __LINE__);
  50. break;
  51. }
  52. }
  53. if (!isset($attrs['show']))
  54. {
  55. $output .= "{$section_props}['show'] = true;\n";
  56. }
  57. if (!isset($attrs['loop']))
  58. {
  59. $output .= "{$section_props}['loop'] = 1;\n";
  60. }
  61. if (!isset($attrs['max']))
  62. {
  63. $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
  64. }
  65. else
  66. {
  67. $output .= "if ({$section_props}['max'] < 0)\n" .
  68. " {$section_props}['max'] = {$section_props}['loop'];\n";
  69. }
  70. if (!isset($attrs['step']))
  71. {
  72. $output .= "{$section_props}['step'] = 1;\n";
  73. }
  74. if (!isset($attrs['start']))
  75. {
  76. $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
  77. }
  78. else
  79. {
  80. $output .= "if ({$section_props}['start'] < 0)\n" .
  81. " {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" .
  82. "else\n" .
  83. " {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
  84. }
  85. $output .= "if ({$section_props}['show']) {\n";
  86. if (!isset($attrs['start']) && !isset($attrs['step']) && !isset($attrs['max']))
  87. {
  88. $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
  89. }
  90. else
  91. {
  92. $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";
  93. }
  94. $output .= " if ({$section_props}['total'] == 0)\n" .
  95. " {$section_props}['show'] = false;\n" .
  96. "} else\n" .
  97. " {$section_props}['total'] = 0;\n";
  98. $output .= "if ({$section_props}['show']):\n";
  99. $output .= "
  100. for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
  101. {$section_props}['iteration'] <= {$section_props}['total'];
  102. {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
  103. $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
  104. $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
  105. $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
  106. $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
  107. $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
  108. $output .= "?>";
  109. return $output;
  110. }
  111. ?>