Are {% set %} Twig variables cached within the {% cache %} tag? #17606
-
So we have this, maybe weird, setup with extends and embeds. And we are wondering if any {% set %} variables are also cached if they are within the {% cache %} tag. Not sure if it's relevant but this is our template setup:
Now on the 404.twig we have the following (stripped down) templating: {% cache globally %} {# This is a Craft CMS tag #}
{% embed "_layout/layout.twig" %}
{% block content %}
{# ... #}
{% endblock %}
{% endembed %}
{% endcache %} The layout.twig looks something like this: {% extends "_variables.twig" %}
{% block html %}
<!DOCTYPE html>
<html>
<head>
{# ... #}
</head>
<body>
{% block main %}
<main class="grow relative z-0">
{% block content %}{% endblock %}
</main>
{% endblock %}
</body>
</html>
{% endblock %} In the _variables.twig we set a lot of different variables used throughout different templates just like the 404.twig. As confirmed in this issue twigphp/Twig#4666 it looks like the {% set %} variables from _variables.twig are included within the I tried looking in the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Beta Was this translation helpful? Give feedback.
{% cache %}
tags will statically cache the returned HTML output, as well as any JavaScript/CSS registered with{% js %}
/{% css %}
, etc.. But there’s no way for them cache any variables defined within them, nor would there be any point. When template rendering encounters a{% cache %}
tag, the tag will simply check to see if its contents are already cached, and if so, return the cached HTML verbatim, rather than execute any nested tags/HTML.