ForLoop.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class Twig_Node_ForLoop extends Twig_Node
  16. {
  17. public function __construct($lineno, $tag = null)
  18. {
  19. parent::__construct(array(), array('with_loop' => false, 'ifexpr' => false, 'else' => false), $lineno, $tag);
  20. }
  21. public function compile(Twig_Compiler $compiler)
  22. {
  23. if ($this->getAttribute('else')) {
  24. $compiler->write("\$context['_iterated'] = true;\n");
  25. }
  26. if ($this->getAttribute('with_loop')) {
  27. $compiler
  28. ->write("++\$context['loop']['index0'];\n")
  29. ->write("++\$context['loop']['index'];\n")
  30. ->write("\$context['loop']['first'] = false;\n")
  31. ;
  32. if (!$this->getAttribute('ifexpr')) {
  33. $compiler
  34. ->write("if (isset(\$context['loop']['length'])) {\n")
  35. ->indent()
  36. ->write("--\$context['loop']['revindex0'];\n")
  37. ->write("--\$context['loop']['revindex'];\n")
  38. ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")
  39. ->outdent()
  40. ->write("}\n")
  41. ;
  42. }
  43. }
  44. }
  45. }