ForLoop.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2011 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Internal node used by the for node.
  12. *
  13. * @package twig
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Node_ForLoop extends Twig_Node
  17. {
  18. public function __construct($lineno, $tag = null)
  19. {
  20. parent::__construct(array(), array('with_loop' => false, 'ifexpr' => false, 'else' => false), $lineno, $tag);
  21. }
  22. /**
  23. * Compiles the node to PHP.
  24. *
  25. * @param Twig_Compiler A Twig_Compiler instance
  26. */
  27. public function compile(Twig_Compiler $compiler)
  28. {
  29. if ($this->getAttribute('else')) {
  30. $compiler->write("\$context['_iterated'] = true;\n");
  31. }
  32. if ($this->getAttribute('with_loop')) {
  33. $compiler
  34. ->write("++\$context['loop']['index0'];\n")
  35. ->write("++\$context['loop']['index'];\n")
  36. ->write("\$context['loop']['first'] = false;\n")
  37. ;
  38. if (!$this->getAttribute('ifexpr')) {
  39. $compiler
  40. ->write("if (isset(\$context['loop']['length'])) {\n")
  41. ->indent()
  42. ->write("--\$context['loop']['revindex0'];\n")
  43. ->write("--\$context['loop']['revindex'];\n")
  44. ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")
  45. ->outdent()
  46. ->write("}\n")
  47. ;
  48. }
  49. }
  50. }
  51. }