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 $linenostring $tag null)
  20.     {
  21.         parent::__construct([], ['with_loop' => false'ifexpr' => false'else' => false], $lineno$tag);
  22.     }
  23.     public function compile(Compiler $compiler): void
  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.                 ->write("if (isset(\$context['loop']['length'])) {\n")
  34.                 ->indent()
  35.                 ->write("--\$context['loop']['revindex0'];\n")
  36.                 ->write("--\$context['loop']['revindex'];\n")
  37.                 ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")
  38.                 ->outdent()
  39.                 ->write("}\n")
  40.             ;
  41.         }
  42.     }
  43. }