twig if one is true and other is false - twig

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

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.

What does Twig's 'default' filter do if no argument is provided?

I understand how the default filter would behave if it were used like so: items|default(posts)
However, I stumbled across some code where it was used but no arg was passed:
{% if ( posts|default ) %}
{% endif %}
It's possible that it's actually not doing anything and is just incomplete or boilerplate code, but I wanted to double-check.
Not passing any arguments the default filter will result in twig returning an empty string (''). It's also worth to mention that in twig, if you test an empty string it will result in false.
So in this case if the post variable is not defined, false or an empty string, the filter will return an empty string and the if will return the value false thus ignoring the code inside the code block
{% set foo = bar|default %}
{{ foo == '' ? 'empty string' : 'not an empty string' }}
{% if '' %}
Do something
{% else %}
Don't do anything
{% endif %}
--------------------------------
{% set var1 = false %}
{% set var2 = {} %}
{% if var1 | default %}
Do sthing with var1
{% else %}
Don't do anything with var1
{% endif %}
{% if var2 | default %}
Do sthing with var2
{% else %}
Don't do anything with var2
{% endif %}
{% if var3 | default %}
Do sthing with var3
{% else %}
Don't do anything with var3
{% endif %}
demo

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

IF a == true OR b == true statement

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

Resources