87

I can't find a way to have TWIG interpret the following conditional statement:

{% if a == true or b == true %}
do stuff
{% endif %}

Am I missing something or it's not possible?

j0k
  • 22,600
  • 28
  • 79
  • 90
MarkL
  • 8,770
  • 7
  • 26
  • 27

2 Answers2

168

check this Twig Reference.

You can do it that simple:

{% if (a or b) %}
    ...
{% endif %}
Andreu Ramos
  • 2,858
  • 2
  • 25
  • 36
36

Comparison expressions should each be in their own brackets:

{% if (a == 'foo') or (b == 'bar') %}
    ...
{% endif %}

Alternative if you are inspecting a single variable and a number of possible values:

{% if a in ['foo', 'bar', 'qux'] %}
    ...
{% endif %}
Tim
  • 6,281
  • 3
  • 39
  • 49
  • not necessarily, according to Twig's official documentation https://twig.symfony.com/doc/2.x/tags/if.html – Luciano Sep 01 '17 at 13:21
  • 1
    [Correct](https://twigfiddle.com/vwy6vu). I had problems in Drupal 8 and had to wrapping the expressions in brackets solved it. – Tim Sep 01 '17 at 16:31
  • @Tim is there a way to abbreviate the following? `{% if (kind == '01') or (kind == '02') or (kind == '03') or (kind == '04') or (kind == '05') %}` – neoDev Oct 14 '17 at 00:38
  • 2
    Use an `in` statement: `{% if kind in ['01', '02'] %} ...`. [Twigfiddle](https://twigfiddle.com/gwktx5). – Tim Oct 16 '17 at 06:27