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