Craft 3 Documentation

{% cache %}

This tag will cache a portion of your template, which can improve performance for subsequent requests, as they will have less work to do.

{% cache %}
    {% for block in entry.myMatrixField.all() %}
        <p>{{ block.text }}</p>
    {% endfor %}
{% endcache %}

Warning: If you’re suffering from abnormal page load times, you may be experiencing a suboptimal hosting environment. Please consult a specialist before trying {% cache %}. {% cache %} is not a substitute for fast database connections, efficient templates, or moderate query counts. Possible side effects include stale content, excessively long-running background tasks, stuck tasks, and in rare cases, death. Ask your hosting provider if {% cache %} is right for you.

Parameters #

The {% cache %} tag supports the following parameters:

globally #

Caches the output globally (for the current site locale), rather than on a per-URL basis.

{% cache globally %}

using key #

Specifies the name of the key the cache should use. If this is not provided, a random key will be generated when Twig first parses the template.

{% cache using key "page-header" %}

Tip: You can combine this parameter with globally to cache templates on a per-page basis, without letting any query string variables get included in the path:

{% cache globally using key craft.request.path %}

Warning: If you change the template code within a {% cache %} that uses a custom key, any existing template caches will not automatically be purged. You will either need to assign the tag a new key, or clear your existing template caches manually using the Clear Caches tool in Settings.

for #

The amount of time it should take for the cache to expire.

{% cache for 3 weeks %}

The accepted duration units are:

Tip: If this parameter is omitted, your cacheDuration config setting will be used to define the default duration.

until #

A DateTime object defining when the cache should expire.

{% cache until entry.eventDate %}

Tip: You can only use for or until in a single {% cache %} tag.

if #

Only activates the {% cache %} tag if a certain condition is met.

{# Only cache if this is a mobile browser #}
{% cache if craft.request.isMobileBrowser() %}

unless #

Prevents the {% cache %} tag from activating if a certain condition is met.

{# Don't cache if someone is logged in #}
{% cache unless currentUser %}

Tip: You can only use if or unless in a single {% cache %} tag.

Cache clearing #

Your caches will automatically clear when any elements (entries, assets, etc.) within the tags are saved or deleted.

If you have any element queries within the tags (e.g. a craft.entries), and you create a new element that should be returned by one of the queries, Craft will also be able to figure that out and clear the cache.

You can also manually clear all of your template caches from the Settings page, using the “Clear Caches” tool.

When to use {% cache %} tags #

You should use {% cache %} tags any time you’ve got a template that’s causing a lot of database queries, or you’re doing something very computationally expensive with Twig.

Here are some examples of when to use them:

There are also some cases where it’s not a good idea to use them:

Tip: The {% cache %} tag will detect if there are any ungenerated image transform URLs within it. If there are, it will hold off on caching the template until the next request, so those temporary image URLs won’t get cached.