Slide 95
Slide 95 text
{# Cache Config #}
{% set cacheflags = cacheflags ?? 'entries|assets|globals|categories|users' %}
{% set cacheKeyPrefix = {
device: craft.app.request.isMobileBrowser ? 'mobile' : 'nonmobile',
user: currentUser ? 'user' : 'guest',
}|join(',') %}
{% set cacheableEnv = craft.app.request.isPost
and not (doNotCache ?? false)
and not craft.app.session.hasFlash('error')
and not craft.app.session.hasFlash('notice')
%}
{# Figure out if page should be cached #}
{%- if cacheableEnv %}
{# If a cacheKey is set, use that to globally cache the rendered page #}
{% if cacheKey ?? false %}
{% cacheflag flagged cacheflags globally using key (cacheKeyPrefix ~ ':' ~ cacheKey) for 1 month %}
{%- minify html %}
{{ block('html') }}
{% endminify -%}
{% endcacheflag %}
{% else %}
{# No cacheKey set, cache the rendered page by url (not globally) #}
{% cacheflag flagged cacheflags using key cacheKeyPrefix for 1 month %}
{%- minify html %}
{{ block('html') }}
{% endminify -%}
{% endcacheflag %}
{% endif %}
{% else %}
{% block html %}{% endblock %}
{% endif %}