Skip to content

Commit

Permalink
Fix the For optimizer, "include" function was always optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
acasademont committed Dec 4, 2014
1 parent efed4fa commit 4319bb9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Twig/NodeVisitor/Optimizer.php
Expand Up @@ -205,6 +205,16 @@ protected function enterOptimizeFor(Twig_NodeInterface $node, Twig_Environment $
$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
Expand Down
10 changes: 10 additions & 0 deletions test/Twig/Tests/NodeVisitor/OptimizerTest.php
Expand Up @@ -89,6 +89,16 @@ public function getTestsForForOptimizer()
array('{% for i in foo %}{% for j in foo %}{{ foo.parent.loop.index }}{% endfor %}{% endfor %}', array('i' => false, 'j' => false)),

array('{% for i in foo %}{% for j in foo %}{{ loop["parent"].loop.index }}{% endfor %}{% endfor %}', array('i' => true, 'j' => true)),

array('{% for i in foo %}{{ include("foo") }}{% endfor %}', array('i' => true)),

array('{% for i in foo %}{{ include("foo", with_context = false) }}{% endfor %}', array('i' => false)),

array('{% for i in foo %}{{ include("foo", with_context = true) }}{% endfor %}', array('i' => true)),

array('{% for i in foo %}{{ include("foo", { "foo": "bar" }, with_context = false) }}{% endfor %}', array('i' => false)),

array('{% for i in foo %}{{ include("foo", { "foo": loop.index }, with_context = false) }}{% endfor %}', array('i' => true)),
);
}

Expand Down

0 comments on commit 4319bb9

Please sign in to comment.