262

I am using Twig as a templating engine. However, I ran in a situation which definitely must be 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 }} does not 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 }} does not 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?

0

8 Answers 8

539

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

{% if myVar is in_array(array_keys(someOtherArray)) %} # or {% if myVar in someOtherArray|keys %} 

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

Sign up to request clarification or add additional context in comments.

4 Comments

If you want to achieve the same as in_array() in PHP, ommit the keys filter
+ 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 %}.
You can also use defined : {% if someOtherArray.myVar is defined %} (twig.sensiolabs.org/doc/tests/defined.html)
Here's a Twig fiddle to play with of the complete solution based upon the OP's code and the code in this answer: twigfiddle.com/0b5crp
151

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

To do the same as PHP in_array, use the following expression:

{% if myVar in myArray %} 

If you want to negate this, you should use this:

{% if myVar not in myArray %} 

Comments

52

Try this:

{% if var in ['foo', 'bar', 'beer'] %} {{ var }} {% endif %} 

Comments

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 %} 

Comments

7

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

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

If someone needs to work through foreach, then:

{% for attachment in attachments %} {{ attachment in item['Attachments'][0] ? 'y' : 'n' }} {% endfor %} 

1 Comment

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.
3
{% 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

1 Comment

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). twig.symfony.com/doc/2.x/deprecated.html#tags
2

The below, without keys, helps me.

{% if myVar in myArray %} 

1 Comment

yep, or even {% if 5 in [3,5] %}
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.