How do I set the following code to false?
{% if loop.first or breadcrumbs|length == loop.index or breadcrumbs|length - 1 == loop.index %}
Related
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 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.
Does anyone know how to loop through an array and find the first of an item then break the loop in twig?
Like this
Loop->find 3
- 2
- 2
- 3 - then break loop here
- 1
- 3
Try this code. It's working on the twig <= 2 version.
{% set break = false %}
{% set numbers = [2,2,3,1,3] %}
{% for number in numbers if not break %}
- {{ number }} <br/>
{% if number == 3 %}
{% set break = true %}
{% endif %}
{% endfor %}
But in Twig 3, it's not working. You can try the below code it's working for twig 3.
{% set break = false %}
{% set numbers = [2,2,3,1,3] %}
{% for number in numbers %}
{% if break == false %}
- {{ number }} <br/>
{% if number == 3 %}
{% set break = true %}
{% endif %}
{% endif %}
{% endfor %}
I have read the twig 3 document but I can't fine break/continue concept on that.
=> Output
- 2
- 2
- 3
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 %}
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 %}