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?
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.
Thus, it is safe to say that . equals() hinders performance and that == is better to use in most cases to compare Boolean .
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.
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 .
check this Twig Reference.
You can do it that simple:
{% if (a or b) %}
...
{% endif %}
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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With