IF a == true OR b == true statement - 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?

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

Related

Opencart 3 TWIG: how to use AND operator

I'm trying to use the "AND" operator in TWIG in Opencart 3 and it doesn't work. Please tell me what I'm doing wrong.
I have some product attributes. And I want to make a condition that if two of the attribute with a specific ID are there, then the condition is met.
{% for attribute_group in attribute_groups %}
{% for attribute in attribute_group.attribute %}
{% if attribute.attribute_id == 2 and attribute.attribute_id == 3 %}
First condition
{% elseif attribute.attribute_id == 2 %}
Second condition
{% elseif attribute.attribute_id == 3 %}
Third condition
{% else %}
{% endif %}
{% endfor %}
{% endfor %
Here is text example:
if there is an attribute with ID equal 2 and an attribute with ID equal 3 then write "Floor/Number of floors".
if there is an attribute with ID equal 2 only then write "Floor"
if there is an attribute with ID equal 3 only then write "Numbers of floors".
Something can't be both X and Y at the same time. Furthermore this is something I'd advice you to test in the controller and not in the view.
Anyway if you wanted to do this in the view you will need to track of the found attributes. You could do this with two booleans or just add a counter.
{% set cnt = 0 %}
{% for attribute_group in attribute_groups %}
{% for attribute in attribute_group.attribute %}
{% if attribute.attribute_id == 2 or attribute.attribute_id == 3 %}
{% set cnt = cnt + 1 %}
{% endif %}
{% endfor %}
{% endfor %}
{% if cnt == 2 %}
{# do something #}
{% endif %}
You can simplify the if by using the test in
{% if attribute.attribute_id in [2, 3,] %}
Update of my answer because OP changed the requirements of the question
{% set words = [] %}
{% for attribute_group in attribute_groups %}
{% for attribute in attribute_group.attribute %}
{% if attribute.attribute_id == 2 %}
{% set words = words|merge(['Floor',]) %}
{% elseif attribute.attribute_id == 3 %}
{% set words = words|merge(['Numbers of floors',]) %}
{% endif %}
{% endfor %}
{% endfor %}
{% if words|default %}
{{ words|join('/') }}
{% endif %}
demo

I want to put OR operator in twig if statement but OR operator not working in Twig

I have written following code but its not working -
{% for key, value in data %}
{% if key == "A" OR key == "A+B" OR key == "A+C" %}
{% set continue = "false" %}
{% endif %}
{% endfor %}
also tried
{% for key, value in data %}
{% if key == "A" || key == "A+B" || key == "A+C" %}
{% set continue = "false" %}
{% endif %}
{% endfor %}
But its not working. What am I doing wrong ?
Variables initiated inside a loop only live in the scope of that loop. U'd need to define the variable outside the loop in order to access it outside the loop.
{% set continue = true %}
{% for key, value in data %}
{% if key == "A" or key == "A+B" or key == "A+C" %}
{% set continue = false %}
{% endif %}
{% endfor %}
{{ continue ? 'continue' : 'dont continue' }}
demo
Note: the correct syntax of or in twig is or, nothing else.

Access variable inside a loop and variable in Twig

I would like to do the following:
{% for i in 0..10 %}
{% if content_{{ i }}_raw == 2 %}
...
{% endif %}
{% endfor %}
Is it possible to get {{ i }} inside the variable content_1_raw and replace the 1 with the value of i?
Yes. The _context variable holds all variables in the current context. You can access its values with the bracket notation or using the attribute function:
{% for i in 0..10 %}
{% if _context['content_' ~ i ~ '_raw'] == 2 %}
...
{% endif %}
{# or #}
{% if attribute(_context, 'content_' ~ i ~ '_raw') == 2 %}
...
{% endif %}
{% endfor %}
I have written more details about this here: Symfony2 - How to access dynamic variable names in twig
Also, instead of writing 'content_' ~ i ~ '_raw' (tilde, ~, is string concatenation operator), you can also use string interpolation:
"content_#{i}_raw"

twig if one is true and other is false

In relation to this question.
What do you do when a == true and b == false? This must be
Before down voting believe it or not but there's nothing to find on this.
So:
{% if a == true and b == false %}
do stuff
{% endif %}
You should say that this should work but that isn't:
{% if (a == true) and (b == false) %}
do stuff
{% endif %}
UPDATE2
This works because one is true and two is false
{% if variant.stock.track == true %}
{% if variant.stock.on_stock == false %}
({{ 'Out of stock' | t }}){% else %} ({{ 'In stock' | t }})
{% endif %}
{% endif %}
Normally when verifying for false I use sameas. In your case:
{% if a and b is sameas(false) %}
However documentation implies you can also use shorthand if's like this:
{% if a and b == false %}
Please note that this check depends on the variable being set. If no variable is set, checking for true or false will fail because the variable will have the value null.
So if you want to check for true or false and want to be sure that if no value is set you get false; you might use default:
{% if a and b|default(false) is sameas(false) %}
or if you prefer the php-style:
{% if a and b|default(false) == false %}
This should work as well:
{% if variant.stock.track and variant.stock.on_stock|default(false) is sameas(false) %}
({{ 'Out of stock' | t }}){% else %} ({{ 'In stock' | t }})
{% endif %}
or
{% if variant.stock.track and variant.stock.on_stock|default(false) == false %}
({{ 'Out of stock' | t }}){% else %} ({{ 'In stock' | t }})
{% endif %}

Testing current value of cycle

I want to render blocks of HTML in alternate orientations. Is this the correct syntax in order to get the current value of cycle?
{% if ( {{ cycle(['odd', 'even']) }} == 'odd' ) %}
foo
{% elseif %}
bar
{% endif %}
cycle(['odd', 'even']) should not be inside {{ }} in the if
statement
cycle() should have a second parameter given that counts the amount of loops
the {% elseif %} should either have a condition or be changed to {% else %}
This is what you should do to get the code to work as you want it to (loop 10 times):
{% for i in 0..9 %}
{% if cycle(['odd', 'even'], i) == 'odd' %}
foo
{% else %}
bar
{% endif %}
{% endfor %}
If you want the for to loop objects you can use loop.index (starts at 1) instead of i:
{% for object in objects %}
{% if cycle(['even', 'odd'], loop.index) == 'odd' %}
foo
{% else %}
bar
{% endif %}
{% endfor %}
or loop.index0 (starts at 0):
{% for object in objects %}
{% if cycle(['odd', 'even'], loop.index0) == 'odd' %}
foo
{% else %}
bar
{% endif %}
{% endfor %}

Resources