Why is the variable currentday not outputing: 2 Condition?
Somehow it is not entering the second IF statement.
{% set currentday = '10.04.2022'|date('d.m.Y') %}
<h1>Today: {{ currentday }}</h1>
{% if currentday >= '01.01.2022' and currentday <= '31.03.2022' %}
<h1>1 Condition </h1>
{% elseif currentday >= '01.04.2022' and currentday <= '30.06.2022' %}
<h1>2 Condition </h1>
{% elseif currentday >= '01.07.2022' and currentday <= '30.09.2022' %}
<h1>3 Condition </h1>
{% elseif currentday >= '01.10.2022' and currentday <= '31.12.2022' %}
<h1>4 Condition </h1>
{% endif %}
If the date is formatted in this way in the variable and if/else then it works like a charm:
{% set currentday = '2022-08-10'|date('Y-m-d') %}
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
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
I'm working with twig and got the date and format working. I have a start date (let's say todays day) and I'd like to print every day into a table cell.
I have my date field in the var datum and I'm able to add 1 day with this. it's working.
{% set datum = date(current_user.cwmon)|date_modify("+1 day")|date('D d.m.y') %}
when I put this into a for loop, I get not the answer I'd like to.
the code itself:
{% for j in 0..6 %}
{% set datum = date(current_user.cwmon)|date_modify("+1 day")|date('D d.m.y') %}
// other code
{{ j }}: {{ datum }}
// other code
{% endfor %}
is there a way to use my var j instead of +1 day?
Whatever I try I get an error.
my desired result:
0: Mon 15.01.19
1: Tue 16.01.19
...
6: Sun 20.01.19
Thank you very much in advance.
apparently the answer is quite simple.
{% for j in 0..6 %}
{% set datum = YOUR_DATE|date_modify("+" ~ j ~ " day")|date('D d.m.y') %}
{% endfor %}
with this, datum has the correct value and adds j to itself.
Another solution is overwriting the datum variable
{% set datum = current_user.cwmon %}
{% for j in 0..6 %}
{% set datum = date(datum)|date_modify("+1 day")|date('D d.m.y') %}
// other code
{{ j }}: {{ datum }}
// other code
{% endfor %}
demo
Below is my code. I want to increase J loop index in between inner loop so I have incremented J variable but it is not working.
`{% for j in 0..(products|length-1) %}
{% for f in 0..(rows-1) %}
{% set j = j + 1 %}
{% endfor %}
{% endfor %}`
Is there any other way to increase loop index?
Its not possible to alter the loop indeces of twig due to the fact of how the loops are compiled
{% for i in 1..5 %} for example gets compiled as
$context['_seq'] = twig_ensure_traversable(range(1, 5));
foreach ($context['_seq'] as $context["_key"] => $context["i"]) {
//..
}
I do have another aproach for you to solve this with twig
{% set rows = 2 %}
{% set items = ((products|length) / rows) | round %}
{% for product in products %}
{% if loop.index0 % items == 0 %}
<div class="row">
{% endif %}
<div class="product">
{{ product }}
</div>
{% if loop.index % items == 0 or loop.last %}
</div>
{% endif %}
{% endfor %}
This is strange. If I use {{ j }}, I get all 12 numbers, but adding the Twig date filter just echos out "Jan" twelve times.
How do I echo out all months of the year? Do I have a create an array instead?
<select>
{% for j in range(1, 12) %}
<option>{{ j|date('M') }}</option>
{% endfor %}
</select>
It's because twig treats j as number of seconds from January 1970 (so it's always January).
From twig documentation:
The date filter accepts strings (it must be in a format supported by
the strtotime function), DateTime instances, or DateInterval
instances.
This should work:
{% for j in range(1, 12) %}
<option>{{ date('2012-' ~ j ~ '-01') |date('M') }}</option>
{% endfor %}
The solution given by Cyprian Throwed me the following error
The function "date" does not exist
So I changed code to
{% for j in 1..12 %}
<option>{{ j |date('2012-' ~ j ~ '-01') |date('M') }}</option>
{% endfor %}
and this worked for me.... Thanks Cyprian
For me:
{% for j in 1..12 %}
<option>{{ '2012-' ~ j ~ '-01' |date('M') }}</option>
{% endfor %}
is working fine.