vendor/twig/twig/src/Node/ForLoopNode.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 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. namespace Twig\Node;
  11. use Twig\Compiler;
  12. /**
  13. * Internal node used by the for node.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class ForLoopNode extends Node
  18. {
  19. public function __construct(int $lineno, string $tag = null)
  20. {
  21. parent::__construct([], ['with_loop' => false, 'ifexpr' => false, 'else' => false], $lineno, $tag);
  22. }
  23. public function compile(Compiler $compiler)
  24. {
  25. if ($this->getAttribute('else')) {
  26. $compiler->write("\$context['_iterated'] = true;\n");
  27. }
  28. if ($this->getAttribute('with_loop')) {
  29. $compiler
  30. ->write("++\$context['loop']['index0'];\n")
  31. ->write("++\$context['loop']['index'];\n")
  32. ->write("\$context['loop']['first'] = false;\n")
  33. ;
  34. if (!$this->getAttribute('ifexpr')) {
  35. $compiler
  36. ->write("if (isset(\$context['loop']['length'])) {\n")
  37. ->indent()
  38. ->write("--\$context['loop']['revindex0'];\n")
  39. ->write("--\$context['loop']['revindex'];\n")
  40. ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")
  41. ->outdent()
  42. ->write("}\n")
  43. ;
  44. }
  45. }
  46. }
  47. }
  48. class_alias('Twig\Node\ForLoopNode', 'Twig_Node_ForLoop');