diff --git a/CHANGELOG b/CHANGELOG index e729ecd3a..db340b559 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 2.10.0 (2019-XX-XX) + * deprecated "if" conditions on "for" tags * added "filter", "map", and "reduce" filters (and support for arrow functions) * fixed partial output leak when a PHP fatal error occurs * optimized context access on PHP 7.4 diff --git a/doc/deprecated.rst b/doc/deprecated.rst index 4bb13398e..bc19efdcc 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -39,6 +39,10 @@ Tags * The ``filter`` tag is deprecated in Twig 2.9. Use the ``apply`` tag instead (the ``Twig\TokenParser\FilterTokenParser`` classes is also deprecated). +* Adding an ``if`` condition on a ``for`` tag is deprecated in Twig 2.10. Use a + ``filter`` filter or an "if" condition inside the "for" body instead (if your condition + depends on a variable updated inside the loop) + Final Classes ------------- diff --git a/src/TokenParser/ForTokenParser.php b/src/TokenParser/ForTokenParser.php index ad3ea8587..1d0c84d7e 100644 --- a/src/TokenParser/ForTokenParser.php +++ b/src/TokenParser/ForTokenParser.php @@ -43,6 +43,8 @@ final class ForTokenParser extends AbstractTokenParser $ifexpr = null; if ($stream->nextIf(/* Token::NAME_TYPE */ 5, 'if')) { + @trigger_error(sprintf('Using an "if" condition on "for" tag is deprecated since Twig 2.10.0, use a "filter" filter or an "if" condition inside the "for" body instead (if your condition depends on a variable updated inside the loop).', __CLASS__), E_USER_DEPRECATED); + $ifexpr = $this->parser->getExpressionParser()->parseExpression(); } diff --git a/test/Twig/Tests/Fixtures/tags/for/condition.test b/test/Twig/Tests/Fixtures/tags/for/condition.legacy.test similarity index 52% rename from test/Twig/Tests/Fixtures/tags/for/condition.test rename to test/Twig/Tests/Fixtures/tags/for/condition.legacy.test index b1ad22fd5..690207e42 100644 --- a/test/Twig/Tests/Fixtures/tags/for/condition.test +++ b/test/Twig/Tests/Fixtures/tags/for/condition.legacy.test @@ -1,5 +1,7 @@ --TEST-- "for" tag takes a condition +--DEPRECATION-- +Using an "if" condition on "for" tag is deprecated since Twig 2.10.0, use a "filter" filter or an "if" condition inside the "for" body instead (if your condition depends on a variable updated inside the loop). --TEMPLATE-- {% for i in 1..5 if i is odd -%} {{ loop.index }}.{{ i }}{{ foo.bar }}