Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF a == true OR b == true statement

Tags:

twig

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?

like image 272
MarkL Avatar asked Nov 29 '11 23:11

MarkL


People also ask

Is true and == true Python?

The Python Boolean type is one of Python's built-in data types. It's used to represent the truth value of an expression. For example, the expression 1 <= 2 is True , while the expression 0 == 1 is False . Understanding how Python Boolean values behave is important to programming well in Python.

Can you compare Booleans with ==?

Thus, it is safe to say that . equals() hinders performance and that == is better to use in most cases to compare Boolean .

What makes an IF statement true?

The "and" operator && takes two boolean values and evaluates to true if both are true. The "or" operator || (two vertical bars) takes two boolean values and evaluates to true if one or the other or both are true.

How do you write a boolean condition in an if statement?

An if statement checks a boolean value and only executes a block of code if that value is true . To write an if statement, write the keyword if , then inside parentheses () insert a boolean value, and then in curly brackets {} write the code that should only execute when that value is true .


2 Answers

check this Twig Reference.

You can do it that simple:

{% if (a or b) %}
    ...
{% endif %}
like image 190
Andreu Ramos Avatar answered Sep 29 '22 02:09

Andreu Ramos


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 %}
like image 34
Tim Avatar answered Sep 29 '22 01:09

Tim