How to cycle only when date changes within array in twig? - php

I am very new to twig and have a problem which I don't know how to solve.
So I have something like this:
{% for entry in entries %}
{% set startDate = entry.begin|date_time %}
% set endDate = entry.end|date_time %}
<tr class="{{ cycle(['odd', 'even'], loop.index0) }}">
<td class="text-nowrap">
{% block date_begin %}{{ entry.begin|date_time }}{% endblock %}
{% if entry.end %}
{% block date_end %}{{ endDatum }}{% endblock %}
{% endif %}
</td>
{% endfor %}
If the entries have the same date I want the <tr> to get the same class, but how do I check if the startDate of Array.1 is the same of Array.2?
As I am not really experienced with twig I can't write much here.

You can't use the function cycle for this use-case.
As you want to keep track of the start_date of a previous entry, you will need to store the date in a (temporary) variable and use that variable to toggle the class when they are not the same date.
{% set is_even = true %}
{% set current_date = null %}
<table>
{% for entry in entries %}
{# store the first start_date in current_date so we can actually compare dates #}
{% if current_date is null %}
{% set current_date = entry.start_date %}
{% endif %}
{# toggle the class when the dates aren't the same #}
{% if current_date|date('U') != entry.start_date|date('U') %}
{% set is_even = not is_even %}
{% set current_date = entry.start_date %}
{% endif %}
<tr class="{{ is_even ? 'even':'odd' }}">
<td class="text-nowrap">
{{ entry.start_date }}
</td>
</tr>
{% endfor %}
</table>
demo

Related

get first item in loop

I am looping through an array and would like to add a class if it is the first item.
I have tried
{% if field|first %}
{% if field.first %}
{% if field.first() %}
{% if loop|first %}
{% if loop.first %}
{% if loop.first() %}
none work, please help. (the reason I don't use css pseudo first is that this is a grid layout and I have am doing responsive layout things with it)
{% for field in row.fields %}
<div class="c-product-table__item {% if field|first %}{{ first_item }}{% endif %} ">
<span class="c-product-table__text">{{ field.text }}</span>
</div>
{% endfor %}
I don't know why twig filters are not working, I solved it another way. I set a var and made it blank after first loop
{% set thiscount = 'first_item' %}
{% for field in row.fields %}
<div class="c-product-table__item {{ thiscount }}">
<span class="c-product-table__text">{{ field.text }}</span>
</div>
{% set thiscount = '' %}
{% endfor %}

How to access rows content type

I'm trying to access the row content type to write an IF condition.
please find below the view twig file.
{% for row in rows %}
{{ row.content }}
{% endfor %}
I try the following but not working.
{% for key, row in rows %}
{% if row._entity.gettype == 'warnings'%}
{{row.content}}
{% else %}
Nothing
{% endif %}
{{kint(row)}}
{% endfor %}
You can use keys this way to iterate through the rows.
{% for key in rows|keys %}
{{ rows[key].content }}
{% endfor %}
Hope this helps ;)

PHP loop: How can I repeat this loop after it happend once

So I have this loop
{% if employees %}
{% set count = 1 %}
{% set multiplier = 1 %}
{% for employeeIndex, employee in employees %}
<tr>
<td>{{ employee.werknemers }}</td>
{% for workdayIndex, workday in workdays %}
{% if (employeeIndex * multiplier) == workdayIndex %}
<td>x</td>
{% if count == (employees|length) %}
{% set multiplier = multiplier * employees|length %}
{% set count = count + 1 %}
{% endif %}
{% else %}
<td></td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
{% endif %}
The result is this :
What I want too happen is for the loop too repeat it self after every employee has a date assigned to them. As u can see the first X begins at 3 and goes down too 13. What I want is the X to begin from 14 again and go down the same way. This should be possible in Twig I believe. Thanks for helping out!!
I did not test this because I can't right now test twig but I think this should work
{% if employees %}
{% set employeeCount = (employees|length) %}
{% for employeeIndex, employee in employees %}
<tr>
<td>{{ employee.werknemers }}</td>
{% for workdayIndex, workday in workdays %}
{% if (workdayIndex - employeeIndex) is divisible by (employeeCount) %}
<td>x</td>
{% else %}
<td></td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
{% endif %}

How to add a BR tag after fourth loop using Twig

I have this loop displaying six links from left to right. I would like to add a break after the fourth link but I'm new at Twig and I don't know how to add it in. Do I need another loop inside the For loop?
{% if contactLinks|length <= 6 %}
{% for link in contactLinks %}
{{ link|raw }}
{% endfor %}
{% elseif contactLinks|length >= 6 %}
{% for link in contactLinks %}
{{ link|raw }}
{% endfor %}
{% endif %}
Twig has a special loop variable that you'll need to use. See here:
http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable
{% for link in contactLinks %}
{{ link|raw }}
{% if loop.index == 4 %}
<br/>
{% endif %}
{% endfor %}
http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable

twig template display articles from specific date

I am making a site using twig php template.
I want to display the articles published in November.
So far I have this:
{% if date(article.published) == date('november') %}
{% for article in articles %}
<li> {{ article.title }} </li>
{% endfor %}
{% endif %}
(Thanks in advance)
You should use IF clause inside of the loop, not outside of it:
{% for article in articles %}
{% if date(article.published) == date('november') %}
<li> {{ article.title }} </li>
{% endif %}
{% endfor %}
You are looking for:
{%
if article.published|date('U') > '2014-11-01'|date('U')
and article.published|date('U') < '2014-12-01'|date('U')
%}
The date('U') filter will convert dates to the timestamp format (number of secs since 1970-01-01), so we can do some dates comparisons from here and filter all dates between 2014-11-01 and 2014-11-31 23:59:59.
And as #Giedrius pointed out, you should put the if inside the loop.
This becomes:
{% for article in articles %}
{%
if article.published|date('U') > '2014-11-01'|date('U')
and article.published|date('U') < '2014-12-01'|date('U')
%}
<li> {{ article.title }} </li>
{% endif %}
{% endfor %}

Categories