2

I've hacked this together to create an event archive listing, and although it works, I'm wondering if it's 'correct' or could be done more succinctly?

{% set pastEvents = craft.entries.section('eventsEntry').orderBy('eventDate') %}
   {% for date, entries in pastEvents | group("pastEvents.eventDate|date('Y')") %}
        <h4>{{ date }}</h4>
            <ul>
                {% for event in pastEvents.all() if event.eventDate <= now %}
                <li>
                    {{ event.eventDate|date('d/m/y') }}
                    {{ event.title }}
                </li>                   
                {% endfor %} 
            </ul>   
   {% endfor %}
1
  • Looks good to me. For the most part if it works for you it's correct. Commented Jun 12, 2018 at 18:45

1 Answer 1

1

Instead of grabbing all entries in the eventsEntry section, then filtering by the current date/time, you could do the filtering on the database side first, so you're returning only the entries you're interested in.

Something like this:

{% set pastEvents = craft.entries.section('eventsEntry').eventdate('<= ' ~ now|atom) %}

{% for date, entries in pastEvents | group("pastEvents.eventdate|date('Y')") %}
    <h4>{{ date }}</h4>

    <ul>
        {% for event in pastEvents.all() %}
            <li>
                {{ event.eventdate|date('d/m/y') }}
                {{ event.title }}
            </li>
        {% endfor %}
    </ul>
{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.