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.
Related
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 %}
I have nested content in a Twig array. I have months, each of which have days:
In my page.twig:
{% set mock = {
main_title: 'Main title',
months:
[
{
sub_title: 'Title 1',
days: [
{
monday: 'Lorum',
tuesday: 'Ipsum'
}
]
},
{
sub_title: 'Title 2',
days: [
{
monday: 'Dolorem',
tuesday: 'Neque'
}
]
}
]
}
%}
{% include "component.twig" %}
I'm trying to print each month's sub title and the day text under it:
<h2>Title 1</h2>
<h3>Lorum</h3>
<h3>Ipsum</h3>
<h2>Title 2</h2>
<h3>Dolorem</h3>
<h3>Neque</h3>
In component.twig:
{% for m in months %}
<h2>{{ m.sub_title }}</h2>
{% for d in months.days %}
<h3>Print test</h3>
{% endfor %}
{% endfor %}
The month's sub_title in <h2> is printing fine but I can't even get the days in the months to loop correctly.
It appears that the mistake is in your second loop. Instead of months.days, you need to use m.days.
Your first loop pulls the month into the variable m. As your main array months does not have an element days, but each individual month does, your inner loop currently has no content to print.
Just as a side note, I would also recommend adding escaping if this template doesn't use autoescape.
{% for m in months %}
<h2>{{m.sub_title| e}}</h2>
{% for d in m.days %}
<h3>{{ d| e }}</h3>
{% endfor %}
{% endfor %}
------edit-----
I missed on first pass that your sample array has an array "days" with a hash inside it instead of being a single level. In this case, you actually have the equivalent (in PHP anyway of an array in an array) for the days key.
This should do the trick in this case
{% for m in months %}
<h2>{{m.sub_title| e}}</h2>
{% for d in m.days[0] %}
<h3>{{ d| e }}</h3>
{% endfor %}
{% endfor %}
So, here's my situation, I have a beginning date and an ending date, with Twig I want to be able to loop through all days in the so called period so that I could print out every day. Of course, that's just for understanding how to do it, the goal is to get them into a chart. Anyway, I have the following code (with what are my vars) :
{% set start_year = date(start) | date('d-m-Y') %}
{% set end_year = date(end)| date('d-m-Y') %}
{% for i in start_year..end_year %}
{{ i }}
{% endfor %}
My start var is 01-01-2003 and my end var is 10-05-2014. The values don't matter as they could change, but that's the format I have.
This actually prints out 0 1 which I don't understand at all. If anyone has an idea either how to do this or how to fix what I'm doing, it would be really nice. Thanks.
You cannot define a range of exact dates (neither as range in php) but you can create a range of seconds with step of a 24 hours second which is 86400; if you use date('U') it will convert date string to seconds since the Unix Epoch (same as Time() in php)
{% set start_date = '01-06-2014' %}
{% set end_date = '05-06-2014' %}
{% for x in range(start_date|date('U'), end_date|date('U'), 86400 ) %}
{{ x|date('d/m/Y') }}<br>
{% endfor %}
Tip
Pay attention the format of date to use - as separator not / because it will lead to totally different result
{% set pd_begdate = header_data.pd_begdate %}
{% set pd_enddate = header_data.pd_enddate ~ ' 23:59:59' %}
{% for key, count_item in range(pd_begdate|date('z'), pd_enddate|date('z'), 1)|slice(column_offset, column_count, 'preserve_keys') %}
{% set item = pd_begdate|date_modify('+'~ key ~' day') %}
<td class="tr5 td9"><p class="p9 ft0">{{ item|date("M") }}<br>{{ item|date("d") }}<br>{{ item|date("D") }}<br>{{ key + 1 }}</p></td>
{% endfor %}
This solution works for day light savings.
I had trouble with the solution when I tried to iterate over an entire year - after about 8 months, I found it would drop a day.
But based on the solution How to add n-days to twig date format within a for-loop?, I found that this would work:
{% set yearstart = "01-01-2003"|date('m-d-Y') %}
{% set eachday = "" %}
{% for i in range(0,365) %}
{% set eachday = yearstart|date_modify("+" ~ i ~ "day")|date('m-d-y') %}
{% endfor %}
I am looking to count the number of entries I have in an array in Twig. This is the code I've tried:
{%for nc in notcount%}
{{ nc|length }}
{%endfor%}
This however only produces the length of the string of one of the values in the array.
{{nc}} will produce an output of all the values of the array (there are 2) but I want the output to be just the number 2 (the count) and not all the information in the array.
Just use the length filter on the whole array. It works on more than just strings:
{{ notcount|length }}
This expands on the answer by Denis Bubnov.
I used this to find child values of array elements—namely if there was a anchor field in paragraphs on a Drupal 8 site to build a table of contents.
{% set count = 0 %}
{% for anchor in items %}
{% if anchor.content['#paragraph'].field_anchor_link.0.value %}
{% set count = count + 1 %}
{% endif %}
{% endfor %}
{% if count > 0 %}
--- build the toc here --
{% endif %}
Best practice of getting length is use length filter returns the number of items of a sequence or mapping, or the length of a string. For example: {{ notcount | length }}
But you can calculate count of elements in for loop. For example:
{% set count = 0 %}
{% for nc in notcount %}
{% set count = count + 1 %}
{% endfor %}
{{ count }}
This solution helps if you want to calculate count of elements by condition, for example you have a property name inside object and you want to calculate count of objects with not empty names:
{% set countNotEmpty = 0 %}
{% for nc in notcount if nc.name %}
{% set countNotEmpty = countNotEmpty + 1 %}
{% endfor %}
{{ countNotEmpty }}
Useful links:
length
set
for
{%for nc in notcount%}
{{ loop.index }}
{%endfor%}
loop.index -- The current iteration of the loop.
for reference:https://twig.symfony.com/doc/2.x/tags/for.html