From 847276a6b961e9a16b74cfeb24b068721d31fb5b Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Mon, 20 May 2019 09:05:23 +0200 Subject: [PATCH 1/4] doc(filter): add example of `filter` without `for` use --- doc/filters/filter.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/filters/filter.rst b/doc/filters/filter.rst index a6c1c132db..9be8912c55 100644 --- a/doc/filters/filter.rst +++ b/doc/filters/filter.rst @@ -29,6 +29,9 @@ function. The arrow function receives the value of the sequence or mapping: {% endfor %} {# output l = 40 xl = 42 #} + {{ sizes|filter(v => v > 38)|join(', ') }} + {# output 40, 42 #} + The arrow function also receives the key as a second argument: .. code-block:: twig From 571929f263d56590ddd6cf42e860264d26f07e51 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 20 May 2019 09:21:53 +0200 Subject: [PATCH 2/4] tweaked docs --- doc/filters/filter.rst | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/doc/filters/filter.rst b/doc/filters/filter.rst index 9be8912c55..d1c18d9fd2 100644 --- a/doc/filters/filter.rst +++ b/doc/filters/filter.rst @@ -11,11 +11,22 @@ function. The arrow function receives the value of the sequence or mapping: {% set sizes = [34, 36, 38, 40, 42] %} + {{ sizes|filter(v => v > 38)|join(', ') }} + {# output 40, 42 #} + +Combined with the ``for`` tag, it allows to filter the itemss to iterate over: + +.. code-block:: twig + {% for v in sizes|filter(v => v > 38) -%} {{ v }} {% endfor %} {# output 40 42 #} +It also works with mappings: + +.. code-block:: twig + {% set sizes = { xs: 34, s: 36, @@ -29,9 +40,6 @@ function. The arrow function receives the value of the sequence or mapping: {% endfor %} {# output l = 40 xl = 42 #} - {{ sizes|filter(v => v > 38)|join(', ') }} - {# output 40, 42 #} - The arrow function also receives the key as a second argument: .. code-block:: twig From a05da6dbbe8eb37262e6cc578948154859e60ad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Wagner?= Date: Tue, 21 May 2019 12:53:40 +0200 Subject: [PATCH 3/4] Add information for when `filter` filter was added in 2.x branch The current "versionadded" info can be confusing to developers, if they assume 2.9 > 1.41 and don't know that 1.x and 2.x branches are still developed simultaneously. --- doc/filters/filter.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/filters/filter.rst b/doc/filters/filter.rst index d1c18d9fd2..c421c586a5 100644 --- a/doc/filters/filter.rst +++ b/doc/filters/filter.rst @@ -2,7 +2,7 @@ ========= .. versionadded:: 1.41 - The ``filter`` filter was added in Twig 1.41. + The ``filter`` filter was added in Twig 1.41 and 2.10, if you're on 2.x. The ``filter`` filter filters elements of a sequence or a mapping using an arrow function. The arrow function receives the value of the sequence or mapping: From cca7007abdda9cbed03289d5a9eeddfbb57c8e00 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 22 May 2019 15:59:27 +0200 Subject: [PATCH 4/4] tweaked docs --- doc/filters/filter.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/filters/filter.rst b/doc/filters/filter.rst index c421c586a5..36ea06ce11 100644 --- a/doc/filters/filter.rst +++ b/doc/filters/filter.rst @@ -2,7 +2,7 @@ ========= .. versionadded:: 1.41 - The ``filter`` filter was added in Twig 1.41 and 2.10, if you're on 2.x. + The ``filter`` filter was added in Twig 1.41 and 2.10. The ``filter`` filter filters elements of a sequence or a mapping using an arrow function. The arrow function receives the value of the sequence or mapping: