-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
``find`` | ||
======== | ||
|
||
.. versionadded:: 3.11 | ||
|
||
The ``find`` filter was added in Twig 3.11. | ||
|
||
The ``find`` filter returns the first element of a sequence matching an arrow | ||
function. The arrow function receives the value of the sequence: | ||
|
||
.. code-block:: twig | ||
{% set sizes = [34, 36, 38, 40, 42] %} | ||
{{ sizes|find(v => v > 38) }} | ||
{# output 40 #} | ||
It also works with mappings: | ||
|
||
.. code-block:: twig | ||
{% set sizes = { | ||
xxs: 32, | ||
xs: 34, | ||
s: 36, | ||
m: 38, | ||
l: 40, | ||
xl: 42, | ||
} %} | ||
{{ sizes|find(v => v > 38) }} | ||
{# output 40 #} | ||
The arrow function also receives the key as a second argument: | ||
|
||
.. code-block:: twig | ||
{{ sizes|find((v, k) => 's' not in k) }} | ||
{# output 38 #} | ||
Note that the arrow function has access to the current context: | ||
|
||
.. code-block:: twig | ||
{% set my_size = 39 %} | ||
{{ sizes|find(v => v >= my_size) }} | ||
{# output 40 #} | ||
Arguments | ||
--------- | ||
|
||
* ``array``: The sequence or mapping | ||
* ``arrow``: The arrow function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--TEST-- | ||
"filter" filter | ||
--TEMPLATE-- | ||
|
||
{{ [1, 2]|find((v) => v > 3) }} | ||
|
||
{{ [1, 5, 3, 4, 5]|find((v) => v > 3) }} | ||
|
||
{{ [1, 5, 3, 4, 5]|find((v) => v > 3) }} | ||
|
||
{{ {a: 1, b: 2, c: 5, d: 8}|find(v => v > 3) }} | ||
|
||
{{ {a: 1, b: 2, c: 5, d: 8}|find((v, k) => (v > 3) and (k != "c")) }} | ||
|
||
{{ [1, 5, 3, 4, 5]|find(v => v > 3) }} | ||
|
||
{{ it|find((v) => v > 3) }} | ||
|
||
{{ ita|find(v => v > 3) }} | ||
|
||
{{ xml|find(x => true) }} | ||
|
||
--DATA-- | ||
return [ | ||
'it' => new \ArrayIterator(['a' => 1, 'b' => 2, 'c' => 5, 'd' => 8]), | ||
'ita' => new Twig\Tests\IteratorAggregateStub(['a' => 1, 'b' => 2, 'c' => 5, 'd' => 8]), | ||
'xml' => new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><doc><elem>foo</elem><elem>bar</elem><elem>baz</elem></doc>'), | ||
] | ||
--EXPECT-- | ||
|
||
|
||
5 | ||
|
||
5 | ||
|
||
5 | ||
|
||
8 | ||
|
||
5 | ||
|
||
5 | ||
|
||
5 | ||
|
||
foo |