File: find.rst

package info (click to toggle)
php-twig 3.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,940 kB
  • sloc: php: 23,320; makefile: 110; sh: 43
file content (57 lines) | stat: -rw-r--r-- 1,018 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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