lib/Twig/lib/Twig/NodeVisitor/Optimizer.php
protected function enterOptimizeFor(Twig_NodeInterface $node, Twig_Environment $env)
    {
        if ($node instanceof Twig_Node_For) {
            // disable the loop variable by default
            $node->setAttribute('with_loop', false);
            array_unshift($this->loops, $node);
            array_unshift($this->loopsTargets, $node->getNode('value_target')->getAttribute('name'));
            array_unshift($this->loopsTargets, $node->getNode('key_target')->getAttribute('name'));
        } elseif (!$this->loops) {
            // we are outside a loop
            return;
        }

        // when do we need to add the loop variable back?

        // the loop variable is referenced for the current loop
        elseif ($node instanceof Twig_Node_Expression_Name && 'loop' === $node->getAttribute('name')) {
            $node->setAttribute('always_defined', true);
            $this->addLoopToCurrent();
        }

        // optimize access to loop targets
        elseif ($node instanceof Twig_Node_Expression_Name && in_array($node->getAttribute('name'), $this->loopsTargets)) {
            $node->setAttribute('always_defined', true);
        }

        // block reference
        elseif ($node instanceof Twig_Node_BlockReference || $node instanceof Twig_Node_Expression_BlockReference) {
            $this->addLoopToCurrent();
        }

        // include without the only attribute
        elseif ($node instanceof Twig_Node_Include && !$node->getAttribute('only')) {
            $this->addLoopToAll();
        }

        // include function without the with_context=false parameter
        elseif ($node instanceof Twig_Node_Expression_Function
            && 'include' === $node->getAttribute('name')
            && (!$node->getNode('arguments')->hasNode('with_context')
                 || false !== $node->getNode('arguments')->getNode('with_context')->getAttribute('value')
               )
        ) {
            $this->addLoopToAll();
        }

        // the loop variable is referenced via an attribute
        elseif ($node instanceof Twig_Node_Expression_GetAttr
            && (!$node->getNode('attribute') instanceof Twig_Node_Expression_Constant
                || 'parent' === $node->getNode('attribute')->getAttribute('value')
               )
            && (true === $this->loops[0]->getAttribute('with_loop')
                || ($node->getNode('node') instanceof Twig_Node_Expression_Name
                    && 'loop' === $node->getNode('node')->getAttribute('name')
                   )
               )
        ) {
            $this->addLoopToAll();
        }
    }
protected function enterOptimizeFor(Twig_NodeInterface $node, Twig_Environment $env)
    {
        if ($node instanceof Twig_Node_For) {
            // disable the loop variable by default
            $node->setAttribute('with_loop', false);
            array_unshift($this->loops, $node);
            array_unshift($this->loopsTargets, $node->getNode('value_target')->getAttribute('name'));
            array_unshift($this->loopsTargets, $node->getNode('key_target')->getAttribute('name'));
        } elseif (!$this->loops) {
            // we are outside a loop
            return;
        }

        // when do we need to add the loop variable back?

        // the loop variable is referenced for the current loop
        elseif ($node instanceof Twig_Node_Expression_Name && 'loop' === $node->getAttribute('name')) {
            $node->setAttribute('always_defined', true);
            $this->addLoopToCurrent();
        }

        // optimize access to loop targets
        elseif ($node instanceof Twig_Node_Expression_Name && in_array($node->getAttribute('name'), $this->loopsTargets)) {
            $node->setAttribute('always_defined', true);
        }

        // block reference
        elseif ($node instanceof Twig_Node_BlockReference || $node instanceof Twig_Node_Expression_BlockReference) {
            $this->addLoopToCurrent();
        }

        // include without the only attribute
        elseif ($node instanceof Twig_Node_Include && !$node->getAttribute('only')) {
            $this->addLoopToAll();
        }

        // include function without the with_context=false parameter
        elseif ($node instanceof Twig_Node_Expression_Function
            && 'include' === $node->getAttribute('name')
            && (!$node->getNode('arguments')->hasNode('with_context')
                 || false !== $node->getNode('arguments')->getNode('with_context')->getAttribute('value')
               )
        ) {
            $this->addLoopToAll();
        }

        // the loop variable is referenced via an attribute
        elseif ($node instanceof Twig_Node_Expression_GetAttr
            && (!$node->getNode('attribute') instanceof Twig_Node_Expression_Constant
                || 'parent' === $node->getNode('attribute')->getAttribute('value')
               )
            && (true === $this->loops[0]->getAttribute('with_loop')
                || ($node->getNode('node') instanceof Twig_Node_Expression_Name
                    && 'loop' === $node->getNode('node')->getAttribute('name')
                   )
               )
        ) {
            $this->addLoopToAll();
        }
    }
protected function enterOptimizeFor ( Twig_NodeInterface $ node , Twig_Environment $ env ) { if ( $ node instanceof Twig_Node_For ) { // disable the loop variable by default $ node -> setAttribute ( 'with_loop' , false ) ; array_unshift ( $ this -> loops , $ node ) ; array_unshift ( $ this -> loopsTargets , $ node -> getNode ( 'value_target' ) -> getAttribute ( 'name' ) ) ; array_unshift ( $ this -> loopsTargets , $ node -> getNode ( 'key_target' ) -> getAttribute ( 'name' ) ) ; } elseif ( ! $ this -> loops ) { // we are outside a loop return ; } // when do we need to add the loop variable back? // the loop variable is referenced for the current loop elseif ( $ node instanceof Twig_Node_Expression_Name && 'loop' === $ node -> getAttribute ( 'name' ) ) { $ node -> setAttribute ( 'always_defined' , true ) ; $ this -> addLoopToCurrent ( ) ; } // optimize access to loop targets elseif ( $ node instanceof Twig_Node_Expression_Name && in_array ( $ node -> getAttribute ( 'name' ) , $ this -> loopsTargets ) ) { $ node -> setAttribute ( 'always_defined' , true ) ; } // block reference elseif ( $ node instanceof Twig_Node_BlockReference || $ node instanceof Twig_Node_Expression_BlockReference ) { $ this -> addLoopToCurrent ( ) ; } // include without the only attribute elseif ( $ node instanceof Twig_Node_Include && ! $ node -> getAttribute ( 'only' ) ) { $ this -> addLoopToAll ( ) ; } // include function without the with_context=false parameter elseif ( $ node instanceof Twig_Node_Expression_Function && 'include' === $ node -> getAttribute ( 'name' ) && ( ! $ node -> getNode ( 'arguments' ) -> hasNode ( 'with_context' ) || false !== $ node -> getNode ( 'arguments' ) -> getNode ( 'with_context' ) -> getAttribute ( 'value' ) ) ) { $ this -> addLoopToAll ( ) ; } // the loop variable is referenced via an attribute elseif ( $ node instanceof Twig_Node_Expression_GetAttr && ( ! $ node -> getNode ( 'attribute' ) instanceof Twig_Node_Expression_Constant || 'parent' === $ node -> getNode ( 'attribute' ) -> getAttribute ( 'value' ) ) && ( true === $ this -> loops [ 0 ] -> getAttribute ( 'with_loop' ) || ( $ node -> getNode ( 'node' ) instanceof Twig_Node_Expression_Name && 'loop' === $ node -> getNode ( 'node' ) -> getAttribute ( 'name' ) ) ) ) { $ this -> addLoopToAll ( ) ; } }
Optimizes "for" tag by removing the "loop" variable creation whenever possible.
Optimizes for tag by removing the loop variable creation whenever possible .