Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {# _init.twig #}
- {#
- Usage
- extend `_init`
- set `doNotCache` to true to exclude a page
- set `cacheKey` to a string to cache a template globally
- (as opposed to by URL). eg: 404 template
- modify `cacheKeyPrefix` to include variation params, if any
- #}
- {# 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 %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement