smarty_internal_compile_for.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile For
  4. *
  5. * Compiles the {for} {forelse} {/for} tags
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile For Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase {
  18. /**
  19. * Compiles code for the {for} tag
  20. *
  21. * Smarty 3 does implement two different sytaxes:
  22. *
  23. * - {for $var in $array}
  24. * For looping over arrays or iterators
  25. *
  26. * - {for $x=0; $x<$y; $x++}
  27. * For general loops
  28. *
  29. * The parser is gereration different sets of attribute by which this compiler can
  30. * determin which syntax is used.
  31. *
  32. * @param array $args array with attributes from parser
  33. * @param object $compiler compiler object
  34. * @param array $parameter array with compilation parameter
  35. * @return string compiled code
  36. */
  37. public function compile($args, $compiler, $parameter)
  38. {
  39. if ($parameter == 0) {
  40. $this->required_attributes = array('start', 'to');
  41. $this->optional_attributes = array('max', 'step');
  42. } else {
  43. $this->required_attributes = array('start', 'ifexp', 'var', 'step');
  44. $this->optional_attributes = array();
  45. }
  46. // check and get attributes
  47. $_attr = $this->getAttributes($compiler, $args);
  48. $output = "<?php ";
  49. if ($parameter == 1) {
  50. foreach ($_attr['start'] as $_statement) {
  51. $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
  52. $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value];\n";
  53. }
  54. $output .= " if ($_attr[ifexp]){ for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$_attr[var]]->value$_attr[step]){\n";
  55. } else {
  56. $_statement = $_attr['start'];
  57. $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
  58. if (isset($_attr['step'])) {
  59. $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = $_attr[step];";
  60. } else {
  61. $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = 1;";
  62. }
  63. if (isset($_attr['max'])) {
  64. $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->total = (int)min(ceil((\$_smarty_tpl->tpl_vars[$_statement[var]]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$_statement[var]]->step)),$_attr[max]);\n";
  65. } else {
  66. $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->total = (int)ceil((\$_smarty_tpl->tpl_vars[$_statement[var]]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$_statement[var]]->step));\n";
  67. }
  68. $output .= "if (\$_smarty_tpl->tpl_vars[$_statement[var]]->total > 0){\n";
  69. $output .= "for (\$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value], \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration = 1;\$_smarty_tpl->tpl_vars[$_statement[var]]->iteration <= \$_smarty_tpl->tpl_vars[$_statement[var]]->total;\$_smarty_tpl->tpl_vars[$_statement[var]]->value += \$_smarty_tpl->tpl_vars[$_statement[var]]->step, \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration++){\n";
  70. $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->first = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == 1;";
  71. $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->last = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == \$_smarty_tpl->tpl_vars[$_statement[var]]->total;";
  72. }
  73. $output .= "?>";
  74. $this->openTag($compiler, 'for', array('for', $compiler->nocache));
  75. // maybe nocache because of nocache variables
  76. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  77. // return compiled code
  78. return $output;
  79. }
  80. }
  81. /**
  82. * Smarty Internal Plugin Compile Forelse Class
  83. *
  84. * @package Smarty
  85. * @subpackage Compiler
  86. */
  87. class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase {
  88. /**
  89. * Compiles code for the {forelse} tag
  90. *
  91. * @param array $args array with attributes from parser
  92. * @param object $compiler compiler object
  93. * @param array $parameter array with compilation parameter
  94. * @return string compiled code
  95. */
  96. public function compile($args, $compiler, $parameter)
  97. {
  98. // check and get attributes
  99. $_attr = $this->getAttributes($compiler, $args);
  100. list($openTag, $nocache) = $this->closeTag($compiler, array('for'));
  101. $this->openTag($compiler, 'forelse', array('forelse', $nocache));
  102. return "<?php }} else { ?>";
  103. }
  104. }
  105. /**
  106. * Smarty Internal Plugin Compile Forclose Class
  107. *
  108. * @package Smarty
  109. * @subpackage Compiler
  110. */
  111. class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase {
  112. /**
  113. * Compiles code for the {/for} tag
  114. *
  115. * @param array $args array with attributes from parser
  116. * @param object $compiler compiler object
  117. * @param array $parameter array with compilation parameter
  118. * @return string compiled code
  119. */
  120. public function compile($args, $compiler, $parameter)
  121. {
  122. // check and get attributes
  123. $_attr = $this->getAttributes($compiler, $args);
  124. // must endblock be nocache?
  125. if ($compiler->nocache) {
  126. $compiler->tag_nocache = true;
  127. }
  128. list($openTag, $compiler->nocache) = $this->closeTag($compiler, array('for', 'forelse'));
  129. if ($openTag == 'forelse') {
  130. return "<?php } ?>";
  131. } else {
  132. return "<?php }} ?>";
  133. }
  134. }
  135. }
  136. ?>