Using {% cache globally %} inside {% cache %} I am currently building a website that consist almost entirely of blocks.

There are quite a few pages that use the same blocks, but sometimes with slightly different content and not always in the same order.

I have built a matrix field to construct each page out of these blocks and most of these blocks have their own template.

Basically I do this:

{% for block in matrix %}

  {% if block.type == 'type1' %}
    {% include 'type1.twig' %}

  {% elseif block.type == 'type2' %}
    {% include 'type2.twig' %}

  {% endif %}
{% endfor %}


Some of these blocks however, also have for loops in them accessing matrix fields.

I was wondering if it is possible to use {% cache %} tags around my for loop that loops through all the basic blocks, and then within my block templates another {% cache %} tag but with the globally parameter.

So in short, can I safely use cache tags inside other cache tags?
So in short, can I safely use cache tags inside other cache tags?


You can nest {% cache %} tags and in the majority of cases it's probably safe.

Some additional reading:

What effect does nesting cache tags have?

Prevent craftcms caching CMS page with dynamic template