251

I am using Twig as templating engine and I am really loving it. However, now I have run in a situation which definitely mustbe accomplishable in a simpler way than I have found.

What I have right now is this:

{% for myVar in someArray %}    
    {% set found = 0 %}
    {% for id, data in someOtherArray %}
        {% if id == myVar %}
            {{ myVar }} exists within someOtherArray.
            {% set found = 1 %} 
        {% endif %}
    {% endfor %}

    {% if found == 0 %}
        {{ myVar }} doesn't exist within someOtherArray.
    {% endif %}
{% endfor %}

What I am looking for is something more like this:

{% for myVar in someArray %}    
    {% if myVar is in_array(array_keys(someOtherArray)) %}
       {{ myVar }} exists within someOtherArray.
    {% else %}
       {{ myVar }} doesn't exist within someOtherArray.
    {% endif %}
{% endfor %}

Is there a way to accomplish this which I haven't seen yet?

If I need to create my own extension, how can I access myVar within the test function?

Thanks for your help!

Manuel
  • 836
  • 13
  • 30
sprain
  • 7,552
  • 6
  • 35
  • 49
  • 6
    Яaffael1984 has the right answer. But what you are trying to do should be done in the controller, not in the view! Format your array and then give a clean thing to the view for the best readability. – Vivien Oct 05 '12 at 13:59
  • 7
    I suppose it really depends on the context which way makes more sense, don't you think? – sprain Oct 05 '12 at 15:00

8 Answers8

529

You just have to change the second line of your second code-block from

{% if myVar is in_array(array_keys(someOtherArray)) %}

to

{% if myVar in someOtherArray|keys %}

in is the containment-operator and keys a filter that returns an arrays keys.

Manuel
  • 836
  • 13
  • 30
Raffael
  • 19,547
  • 15
  • 82
  • 160
  • 123
    If you want to achieve the same as in_array() in PHP, ommit the keys filter – Burgi Feb 08 '12 at 16:05
  • 28
    + 1 And also the negation is `{% if item not in array %}` and not `{% if not _entry.id in array %}`, so it's different from this `{% if not var is null %}`. – insertusernamehere Dec 18 '12 at 11:23
  • 9
    You can also use defined : `{% if someOtherArray.myVar is defined %}` (http://twig.sensiolabs.org/doc/tests/defined.html) – tight Jun 15 '14 at 18:01
  • Here's a Twig fiddle to play with of the complete solution based upon the OP's code and the code in this answer: https://twigfiddle.com/0b5crp – 7ochem May 28 '21 at 10:14
138

Just to clear some things up here. The answer that was accepted does not do the same as PHP in_array.

To do the same as PHP in_array use following expression:

{% if myVar in myArray %}

If you want to negate this you should use this:

{% if myVar not in myArray %}
Wim Mostmans
  • 3,485
  • 1
  • 20
  • 20
43

Try this

{% if var in ['foo', 'bar', 'beer'] %}
    ...
{% endif %}
Arthur Veselov
  • 1,167
  • 10
  • 9
11

another example following @jake stayman:

{% for key, item in row.divs %}
    {% if (key not in [1,2,9]) %} // eliminate element 1,2,9
        <li>{{ item }}</li>
    {% endif %}
{% endfor %}
William Isted
  • 11,641
  • 4
  • 30
  • 45
Dung
  • 19,199
  • 9
  • 59
  • 54
5

Though The above answers are right, I found something more user-friendly approach while using ternary operator.

{{ attachment in item['Attachments'][0] ? 'y' : 'n' }}

If someone need to work through foreach then,

{% for attachment in attachments %}
    {{ attachment in item['Attachments'][0] ? 'y' : 'n' }}
{% endfor %}
sh6210
  • 4,190
  • 1
  • 37
  • 27
  • 1
    Don't know why this answer gets no upvotes. While this one-liner is the smallest and perfect code you can have nowadays. For example using `{{ item.value in cfg.static_site ? ' checked="checked"' : '' }}` in a for loop (checking if a specific item exists in that array) will check that item. – OSWorX Nov 29 '20 at 08:40
  • @OSWorX agree :) – Meow Kim Oct 15 '21 at 10:20
  • Thank you OSWorX, Meow Kim – sh6210 Oct 15 '21 at 11:27
3

It should help you.

{% for user in users if user.active and user.id not 1 %}
   {{ user.name }}
{% endfor %}

More info: http://twig.sensiolabs.org/doc/tags/for.html

FDisk
  • 8,493
  • 2
  • 47
  • 52
  • 2
    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). https://twig.symfony.com/doc/2.x/deprecated.html#tags – Maxim Mandrik Jan 12 '21 at 14:24
1

{% if myVar in myArray %} without keys helps me

Al Kativo
  • 135
  • 2
  • 9
0

Here's one to complete the answers with all the possibilities of Twig these days:

To achieve something like this:

{% for myVar in someArray %}    
    {% if myVar in someOtherArray|keys %}
       {{ myVar }} exists within someOtherArray.
    {% else %}
       {{ myVar }} doesn't exist within someOtherArray.
    {% endif %}
{% endfor %}

(https://twigfiddle.com/0b5crp)

You could also use array mapping and have the following one-liner:
(Twig >= 1.41 or >= 2.10 or any 3.x version)

{{ someArray|map(myVar => myVar ~ (myVar not in someOtherArray|keys ? ' doesn\'t') ~ ' exists within someOtherArray.')|join('\n') }}

Which outputs something quite similar.

Also see this Twig fiddle: https://twigfiddle.com/dlxj9g

7ochem
  • 2,183
  • 1
  • 34
  • 42