3

I have an archive of an event based site. I know how to break cache until the next event. No Problem

{% cache until nextEvent %}

But the archive works the following way:

archive
-- 2014 (latest year)
-- 2013
-- 2012
-- 2011

The until nextEvent part makes only sense for the latest year. The other years should be cached forever.

How could I achieve such a thing?

-- edit --

Maybe I have to go a little more into the details:
I get the year in the forth segment.
Now it should cache the whole loop and never break it, unless it's the actual year, then break it each night:

{% set latestYear = now|date("Y") %}

{% if year is not defined %}
    {% redirect "club/programm/archiv/"~latestYear %}
{% endif %}

{% set year = craft.request.getSegment(4) %}

{# Set the first day of the year and the last #}
{% set lastyear = year -1 %}
{% set nextyear = year +1 %}
{% set yearbegins = '1 January ' ~ year %}
{% set yearends = '1 January ' ~ nextyear %}
{% set unixbegins = yearbegins | date('U') %}
{% set unixends = yearends | date('U') %}

{# Today at 00:00 #}
{% set today = now|date("Y-m-d") ~ ' 00:00' %}
{% set tomorrow = now|date_modify("+1 day")|date("Y-m-d") ~ ' 00:00' %}

{% set entriesByYear = craft.entries.section('programm').limit(null).cf_date('and', '>= ' ~ unixbegins, '< ' ~ unixends, '< ' ~ today| date("U")) %} {# '> just to make syntax highlighting correct #}

{% cache until tomorrow %}
    {% for month, entries in entriesByYear | group("postDate|date('F')") %}

        {# MONTHLY HEADER #}
        <div class="program_monthly_header">
            <div class="content_column">
                <p>{{ month }} {{ year }} </p>
            </div>
        </div>

        {# ENTRIES PER MONTH #}
        {% for entry in entries %}

            <div class="listing_item{% if loop.last %} last{% endif %}" id="programm_{{ entry.slug }}"> 
                {% include "programm/_common" %}                    
            </div>
            <div class="clear"></div>
        {% endfor %}
    {% endfor %}
{% endcache %}

maybe that helps someone to help me ;)
cheers
stefan

--- edit 2 and solution ---

so after the comments of victor I updated the code. and it works like a charm:

{% set latestYear = now|date("Y") %}

{% if year is not defined %}
    {% redirect "club/programm/archiv/"~latestYear %}
{% endif %}

{% set year = craft.request.getSegment(4) %}

{# Set the first day of the year and the last #}
{% set lastyear = year -1 %}
{% set nextyear = year +1 %}
{% set yearbegins = '1 January ' ~ year %}
{% set yearends = '1 January ' ~ nextyear %}
{% set unixbegins = yearbegins | date('U') %}
{% set unixends = yearends | date('U') %}

{# Today at 00:00 #}
{% set today = now|date("Y-m-d") ~ ' 00:00' %}
{% set tomorrow = now|date_modify("+1 day")|date("Y-m-d") ~ ' 00:00' %}

{# Get the Date of the next Event, to allow caching until then #}
{% set nextEvent = craft.entries.section('programm').cf_date('>= ' ~ today| date("U")).order('cf_date asc').first() %}{# '> just to make syntax highlighting correct #}
{% set nextEventDate = nextEvent.cf_date|date_modify("+1 day")|date("Y-m-d") ~ ' 00:00' %}

{# Get all the events within the year concerned #}  
{% set entriesByYear = craft.entries.section('programm').limit(null).cf_date('and', '>= ' ~ unixbegins, '< ' ~ unixends, '< ' ~ today| date("U")) %} {# '> just to make syntax highlighting correct #}

{# If the year is the current year, cache it until the next event. Otherwhise, cache indefinitely #}
{% if year == latestYear %}
    {% cache until nextEventDate %}
        {% include "club/programm/_archive_common" %}
    {% endcache %}
{% else %}
    {% cache %}
        {% include "club/programm/_archive_common" %}
    {% endcache %}
{% endif %}

thanks for all the fish!
stefan

1 Answer 1

1

You could use the new Cold Cache pluging from P&T which:

Adds a new {% coldcache %} tag to Craft, which works just like Craft’s built-in {% cache %} tag, except that this tag won’t record which elements and element queries are active between its tags. The only way caches will ever get deleted is if they expire, or an admin clears them using the Clear Caches tool in Settings.

6
  • I know that coldcache exists, but that doesn't solve the problem, or does it?
    – outline4
    Commented Sep 9, 2014 at 16:00
  • I don't know how your template looks, but I would create two for-loops: One for the current year (and wrap it in {% cache until nextEvent %}) and one for everything else ({% coldcache %}).
    – Victor
    Commented Sep 9, 2014 at 16:05
  • just after posting my remark, I understood what you meant... I've updated the question as well. Two loops would be an option - you are completely right... I wish there was a way with only one loop, but I guess it's OK like you proposed... btw: don't really need coldcache, I just don't want to break it every night... cheers
    – outline4
    Commented Sep 9, 2014 at 16:08
  • @outline4 Now I understand that you have a separate page for each year. Why don't you just use {% cache %}? It will be saved for each page separately and only cleared when an entry matching that query gets updated.
    – Victor
    Commented Sep 10, 2014 at 11:05
  • no, since it's event based, the events are only going into the archive after a certain date, not when they are edited! that's what it's all about... I will update the question with the answer. that I actually found because of your initial comment ;)
    – outline4
    Commented Sep 10, 2014 at 13:36

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.