Cache Tag

Cache content in your templates using the cache tag.

Most basic cache

{% cache %}

    {% set events = enact.entry.sectionHandle('events').sort('publishOn DESC').limit(10).fetch %}

    <ul>

    {% for event in events %}

        <li>{{ event }}</li>

    {% endfor %}

    </ul>

{% endcache %}

This will cache the content for the amount of time specified by your cacheDuration configuration setting.

Controlling caching

  • key - The key to cache the content with, changing the key will automatically force the cache to be cleared and reset on the next template load. If you don't provide a key the key will be determined by performing a hash on the content of the cache tag. This means that if you change the markup or logic inside the cache tag when no key is set, the cache will automatically be cleared and reset on the next template load.

  • for - How long the the content should be cached for. Specify a DateTime modified string like +10 minutes, +2 weeks, +3 hours.

  • until - (mutually exclusive with for) When the content should be cached until as a DateTime format like 2016-03-17.

  • if - A condition which must be true for the caching to be performed.

  • unless - (mutually exclusive with if) A condition which must be false for the caching to be performed.

Examples with further controls

{% cache key 'my-cache-key' if enact.currentUser for '+1 hour' %}

    // your cache logic

{% endcache %}


{% shouldCache = enact.request.uri != '/' %}

{% cache unless shouldCache until '2016-04-20' %}

    // your cache logic

{% endcache %}