Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. {# _init.twig #}
  2. {#
  3. Usage
  4.  
  5. extend `_init`
  6. set `doNotCache` to true to exclude a page
  7. set `cacheKey` to a string to cache a template globally
  8. (as opposed to by URL). eg: 404 template
  9. modify `cacheKeyPrefix` to include variation params, if any
  10. #}
  11.  
  12. {# Cache Config #}
  13. {% set cacheflags = cacheflags ?? 'entries|assets|globals|categories|users' %}
  14. {% set cacheKeyPrefix = {
  15. device: craft.app.request.isMobileBrowser ? 'mobile' : 'nonmobile',
  16. user: currentUser ? 'user' : 'guest',
  17. }|join(',') %}
  18. {% set cacheableEnv = craft.app.request.isPost
  19. and not (doNotCache ?? false)
  20. and not craft.app.session.hasFlash('error')
  21. and not craft.app.session.hasFlash('notice')
  22. %}
  23.  
  24.  
  25. {# Figure out if page should be cached #}
  26. {%- if cacheableEnv %}
  27.  
  28. {# If a cacheKey is set, use that to globally cache the rendered page #}
  29. {% if cacheKey ?? false %}
  30. {% cacheflag flagged cacheflags globally using key (cacheKeyPrefix ~ ':' ~ cacheKey) for 1 month %}
  31. {%- minify html %}
  32. {{ block('html') }}
  33. {% endminify -%}
  34. {% endcacheflag %}
  35.  
  36. {% else %}
  37.  
  38. {# No cacheKey set, cache the rendered page by url (not globally) #}
  39. {% cacheflag flagged cacheflags using key cacheKeyPrefix for 1 month %}
  40. {%- minify html %}
  41. {{ block('html') }}
  42. {% endminify -%}
  43. {% endcacheflag %}
  44. {% endif %}
  45.  
  46. {% else %}
  47. {% block html %}{% endblock %}
  48. {% endif %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement