From 6302705a1f80f9070bad0f3fd6b8b351ea71abc7 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 28 Dec 2010 19:28:29 +0100 Subject: [PATCH] removed 'joined by' support for the 'for' tag as a tag should not output something directly --- CHANGELOG | 1 + doc/templates.rst | 16 ---------------- lib/Twig/Node/For.php | 19 ++++--------------- lib/Twig/TokenParser/For.php | 9 +-------- test/Twig/Tests/Fixtures/tags/for/joined_by.test | 8 -------- test/Twig/Tests/Node/ForTest.php | 16 +++++----------- 6 files changed, 11 insertions(+), 58 deletions(-) delete mode 100644 test/Twig/Tests/Fixtures/tags/for/joined_by.test diff --git a/CHANGELOG b/CHANGELOG index f566917..a397469 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,6 +6,7 @@ Backward incompatibilities: * the range filter has been converted to a function: 0|range(10) -> range(0, 10) * the constant filter has been converted to a function: {{ some_date|date('DATE_W3C'|constant) }} -> {{ some_date|date(constant('DATE_W3C')) }} * the cycle filter has been converted to a function: {{ ['odd', 'even']|cycle(i) }} -> {{ cycle(['odd', 'even'], i) }} + * the for tag does not support "joined by" anymore Changes: diff --git a/doc/templates.rst b/doc/templates.rst index c7d477f..fc88b27 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -586,22 +586,6 @@ You can also access both keys and values: On Twig before 0.9.3, you need to use the ``items`` filter to access both the keys and values (``{% for key, value in users|items %}``). -To conveniently display comma-separated lists or things alike, you can use -``joined by ", "`` at the end of the loop statement (as of Twig 0.9.10). Of -course this is not limited to commas: - -.. code-block:: jinja - -

Members

-

{% for user in users joined by ", " %}{{ user }}{% endfor %}

- - {# generates a string like

Fabien, Jordi, Thomas, Lucas

#} - -.. note:: - - This way you don't have to check if the item is the last one, the comma - will only be added in between two items. - If ~~ diff --git a/lib/Twig/Node/For.php b/lib/Twig/Node/For.php index ec48f28..ee0d0e0 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_NodeInterface $body, Twig_NodeInterface $else = null, Twig_Node_Expression $joinedBy = null, $lineno, $tag = null) + public function __construct(Twig_Node_Expression_AssignName $keyTarget, Twig_Node_Expression_AssignName $valueTarget, Twig_Node_Expression $seq, Twig_NodeInterface $body, Twig_NodeInterface $else = null, $lineno, $tag = null) { - parent::__construct(array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body, 'else' => $else, 'joined_with' => $joinedBy), 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), $lineno, $tag); } /** @@ -39,7 +39,7 @@ class Twig_Node_For extends Twig_Node ->raw(");\n") ; - if (null !== $this->getNode('else') || null !== $this->getNode('joined_with')) { + if (null !== $this->getNode('else')) { $compiler->write("\$context['_iterated'] = false;\n"); } @@ -72,20 +72,9 @@ class Twig_Node_For extends Twig_Node ->indent() ; - if (null !== $this->getNode('joined_with')) { - $compiler - ->write("if (\$context['_iterated']) {\n") - ->indent() - ->write("echo ") - ->subcompile($this->getNode('joined_with')) - ->raw(";\n") - ->outdent() - ->write("}\n"); - } - $compiler->subcompile($this->getNode('body')); - if (null !== $this->getNode('else') || null !== $this->getNode('joined_with')) { + if (null !== $this->getNode('else')) { $compiler->write("\$context['_iterated'] = true;\n"); } diff --git a/lib/Twig/TokenParser/For.php b/lib/Twig/TokenParser/For.php index 6f4477b..8364362 100644 --- a/lib/Twig/TokenParser/For.php +++ b/lib/Twig/TokenParser/For.php @@ -25,13 +25,6 @@ class Twig_TokenParser_For extends Twig_TokenParser $this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, 'in'); $seq = $this->parser->getExpressionParser()->parseExpression(); - $joinedBy = null; - if ($this->parser->getStream()->test('joined')) { - $this->parser->getStream()->next(); - $this->parser->getStream()->expect('by'); - $joinedBy = $this->parser->getExpressionParser()->parseExpression(); - } - $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideForFork')); if ($this->parser->getStream()->next()->getValue() == 'else') { @@ -50,7 +43,7 @@ class Twig_TokenParser_For extends Twig_TokenParser $valueTarget = $targets->getNode(0); } - return new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $joinedBy, $lineno, $this->getTag()); + return new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $lineno, $this->getTag()); } public function decideForFork($token) diff --git a/test/Twig/Tests/Fixtures/tags/for/joined_by.test b/test/Twig/Tests/Fixtures/tags/for/joined_by.test deleted file mode 100644 index 69b087b..0000000 --- a/test/Twig/Tests/Fixtures/tags/for/joined_by.test +++ /dev/null @@ -1,8 +0,0 @@ ---TEST-- -"for" tag support the "joined by" clause ---TEMPLATE-- -{% for item in items joined by ', ' %}{{ item }}{% endfor %} ---DATA-- -return array('items' => array('a', 'b', 'c')) ---EXPECT-- -a, b, c diff --git a/test/Twig/Tests/Node/ForTest.php b/test/Twig/Tests/Node/ForTest.php index dc1a785..b1a1457 100644 --- a/test/Twig/Tests/Node/ForTest.php +++ b/test/Twig/Tests/Node/ForTest.php @@ -23,7 +23,7 @@ class Twig_Tests_Node_ForTest extends Twig_Tests_Node_TestCase $seq = new Twig_Node_Expression_Name('items', 0); $body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); $else = null; - $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, null, 0); + $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, 0); $node->setAttribute('with_loop', false); $this->assertEquals($keyTarget, $node->getNode('key_target')); @@ -33,7 +33,7 @@ class Twig_Tests_Node_ForTest extends Twig_Tests_Node_TestCase $this->assertEquals(null, $node->getNode('else')); $else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); - $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, null, 0); + $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, 0); $node->setAttribute('with_loop', false); $this->assertEquals($else, $node->getNode('else')); } @@ -56,7 +56,7 @@ class Twig_Tests_Node_ForTest extends Twig_Tests_Node_TestCase $seq = new Twig_Node_Expression_Name('items', 0); $body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); $else = null; - $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, null, 0); + $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, 0); $node->setAttribute('with_loop', false); $tests[] = array($node, <<setAttribute('with_loop', true); $tests[] = array($node, << \$context['_parent'], 'index0' => 0, @@ -98,11 +96,7 @@ if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_ \$context['loop']['last'] = 1 === \$length; } foreach (\$context['_seq'] as \$context['k'] => \$context['v']) { - if (\$context['_iterated']) { - echo ", "; - } echo (isset(\$context['foo']) ? \$context['foo'] : null); - \$context['_iterated'] = true; ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; @@ -123,7 +117,7 @@ EOF $seq = new Twig_Node_Expression_Name('values', 0); $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); - $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, null, 0); + $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, 0); $node->setAttribute('with_loop', true); $tests[] = array($node, <<