From: Fabien Potencier Date: Fri, 4 Nov 2011 12:32:43 +0000 (+0100) Subject: fixed for tag when using a condition with strict_variables set to false X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=23813770de4fcbb7225641aad7383e38712dc186;p=web%2Fkonrad%2Ftwig.git fixed for tag when using a condition with strict_variables set to false --- diff --git a/lib/Twig/Node/For.php b/lib/Twig/Node/For.php index eb204e2..a24f92b 100644 --- a/lib/Twig/Node/For.php +++ b/lib/Twig/Node/For.php @@ -18,9 +18,9 @@ */ class Twig_Node_For extends Twig_Node { - public function __construct(Twig_Node_Expression_AssignName $keyTarget, Twig_Node_Expression_AssignName $valueTarget, Twig_Node_Expression $seq, Twig_Node_Expression $ifexpr = null, Twig_NodeInterface $body, Twig_NodeInterface $else = null, $lineno, $tag = null) + public function __construct(Twig_Node_Expression_AssignName $keyTarget, Twig_Node_Expression_AssignName $valueTarget, Twig_Node_Expression $seq, $ifexpr, Twig_NodeInterface $body, Twig_NodeInterface $else = null, $lineno, $tag = null) { - parent::__construct(array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'ifexpr' => $ifexpr, 'body' => $body, 'else' => $else), array('with_loop' => true), $lineno, $tag); + parent::__construct(array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body, 'else' => $else), array('with_loop' => true, 'ifexpr' => (Boolean) $ifexpr), $lineno, $tag); } /** @@ -53,7 +53,7 @@ class Twig_Node_For extends Twig_Node ->write(");\n") ; - if (null === $this->getNode('ifexpr')) { + if (!$this->getAttribute('ifexpr')) { $compiler ->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n") ->indent() @@ -77,18 +77,6 @@ class Twig_Node_For extends Twig_Node ->indent() ; - if (null !== $this->getNode('ifexpr')) { - $compiler - ->write("if (!(") - ->subcompile($this->getNode('ifexpr')) - ->raw(")) {\n") - ->indent() - ->write("continue;\n") - ->outdent() - ->write("}\n\n") - ; - } - $compiler->subcompile($this->getNode('body')); if (null !== $this->getNode('else')) { @@ -102,7 +90,7 @@ class Twig_Node_For extends Twig_Node ->write("\$context['loop']['first'] = false;\n") ; - if (null === $this->getNode('ifexpr')) { + if (!$this->getAttribute('ifexpr')) { $compiler ->write("if (isset(\$context['loop']['length'])) {\n") ->indent() diff --git a/lib/Twig/TokenParser/For.php b/lib/Twig/TokenParser/For.php index 39755a4..1432785 100644 --- a/lib/Twig/TokenParser/For.php +++ b/lib/Twig/TokenParser/For.php @@ -64,7 +64,11 @@ class Twig_TokenParser_For extends Twig_TokenParser $valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine()); } - return new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this->getTag()); + if (null !== $ifexpr) { + $body = new Twig_Node_If(new Twig_Node(array($ifexpr, $body)), null, $lineno, $this->getTag()); + } + + return new Twig_Node_For($keyTarget, $valueTarget, $seq, null !== $ifexpr, $body, $else, $lineno, $this->getTag()); } public function decideForFork(Twig_Token $token) diff --git a/test/Twig/Tests/Fixtures/tags/for/condition.test b/test/Twig/Tests/Fixtures/tags/for/condition.test new file mode 100644 index 0000000..6879903 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/for/condition.test @@ -0,0 +1,12 @@ +--TEST-- +"for" tag takes a condition +--TEMPLATE-- +{% for i in [1, 2, 3, 4, 5] if i == 2 %} + Hello World! +{% endfor %} +--DATA-- +return array() +--CONFIG-- +return array('strict_variables' => false) +--EXPECT-- +Hello World! diff --git a/test/Twig/Tests/Node/ForTest.php b/test/Twig/Tests/Node/ForTest.php index d6c5470..4dda06e 100644 --- a/test/Twig/Tests/Node/ForTest.php +++ b/test/Twig/Tests/Node/ForTest.php @@ -21,7 +21,7 @@ class Twig_Tests_Node_ForTest extends Twig_Tests_Node_TestCase $keyTarget = new Twig_Node_Expression_AssignName('key', 0); $valueTarget = new Twig_Node_Expression_AssignName('item', 0); $seq = new Twig_Node_Expression_Name('items', 0); - $ifexpr = new Twig_Node_Expression_Constant(true, 0); + $ifexpr = true; $body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); $else = null; $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 0); @@ -30,7 +30,7 @@ class Twig_Tests_Node_ForTest extends Twig_Tests_Node_TestCase $this->assertEquals($keyTarget, $node->getNode('key_target')); $this->assertEquals($valueTarget, $node->getNode('value_target')); $this->assertEquals($seq, $node->getNode('seq')); - $this->assertEquals($ifexpr, $node->getNode('ifexpr')); + $this->assertTrue($node->getAttribute('ifexpr')); $this->assertEquals($body, $node->getNode('body')); $this->assertEquals(null, $node->getNode('else')); @@ -119,40 +119,6 @@ EOF $keyTarget = new Twig_Node_Expression_AssignName('k', 0); $valueTarget = new Twig_Node_Expression_AssignName('v', 0); $seq = new Twig_Node_Expression_Name('values', 0); - $ifexpr = new Twig_Node_Expression_Constant(true, 0); - $body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); - $else = null; - $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 0); - $node->setAttribute('with_loop', true); - - $tests[] = array($node, <<getContext(\$context, "values")); -\$context['loop'] = array( - 'parent' => \$context['_parent'], - 'index0' => 0, - 'index' => 1, - 'first' => true, -); -foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { - if (!(true)) { - continue; - } - - echo \$this->getContext(\$context, "foo"); - ++\$context['loop']['index0']; - ++\$context['loop']['index']; - \$context['loop']['first'] = false; -} -\$_parent = \$context['_parent']; -unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']); -\$context = array_merge(\$_parent, array_intersect_key(\$context, \$_parent)); -EOF - ); - - $keyTarget = new Twig_Node_Expression_AssignName('k', 0); - $valueTarget = new Twig_Node_Expression_AssignName('v', 0); - $seq = new Twig_Node_Expression_Name('values', 0); $ifexpr = null; $body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); $else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0);