PageRenderTime 45ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/smarty/sysplugins/smarty_internal_compile_for.php

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