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>