/framework/vendor/smarty3/lib/libs/sysplugins/smarty_internal_compile_for.php

http://zoop.googlecode.com/ · PHP · 133 lines · 69 code · 7 blank · 57 comment · 16 complexity · 37da9e42a61db3d8cf012d0af280de71 MD5 · raw file

  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. $this->_open_tag('for', array('for', $this->compiler->nocache));
  46. // maybe nocache because of nocache variables
  47. $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
  48. $output = "<?php ";
  49. if (isset($_attr['ifexp'])) {
  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[varloop]]->value$_attr[loop]){\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 = ($_attr[to] - ($_statement[value]) < 0) ? -1 : 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. // return compiled code
  75. return $output;
  76. }
  77. }
  78. /**
  79. * Smarty Internal Plugin Compile Forelse Class
  80. */
  81. class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase {
  82. /**
  83. * Compiles code for the {forelse} tag
  84. *
  85. * @param array $args array with attributes from parser
  86. * @param object $compiler compiler object
  87. * @return string compiled code
  88. */
  89. public function compile($args, $compiler)
  90. {
  91. $this->compiler = $compiler;
  92. // check and get attributes
  93. $_attr = $this->_get_attributes($args);
  94. list($_open_tag, $nocache) = $this->_close_tag(array('for'));
  95. $this->_open_tag('forelse', array('forelse', $nocache));
  96. return "<?php }} else { ?>";
  97. }
  98. }
  99. /**
  100. * Smarty Internal Plugin Compile Forclose Class
  101. */
  102. class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase {
  103. /**
  104. * Compiles code for the {/for} tag
  105. *
  106. * @param array $args array with attributes from parser
  107. * @param object $compiler compiler object
  108. * @return string compiled code
  109. */
  110. public function compile($args, $compiler)
  111. {
  112. $this->compiler = $compiler;
  113. // check and get attributes
  114. $_attr = $this->_get_attributes($args);
  115. // must endblock be nocache?
  116. if ($this->compiler->nocache) {
  117. $this->compiler->tag_nocache = true;
  118. }
  119. list($_open_tag, $this->compiler->nocache) = $this->_close_tag(array('for', 'forelse'));
  120. if ($_open_tag == 'forelse')
  121. return "<?php } ?>";
  122. else
  123. return "<?php }} ?>";
  124. }
  125. }
  126. ?>