smarty_internal_compile_for.php 5.9 KB

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