<blockquote>
  <p>So in short, can I safely use cache tags inside other cache tags?</p>
</blockquote>

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

<p>Some additional reading:</p>

<p><a href="https://craftcms.stackexchange.com/questions/9196/what-effect-does-nesting-cache-tags-have">What effect does nesting cache tags have?</a></p>

<p><a href="https://craftcms.stackexchange.com/questions/14417/prevent-craftcms-caching-cms-page-with-dynamic-template/14418#14418">Prevent craftcms caching CMS page with dynamic template</a></p>

CC BY-SA 4.0
2018-08-24T04:41:42.263Z
2018-08-24T04:41:42.263Z
Brad Bell
<p>I am currently building a website that consist almost entirely of blocks.</p>

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

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

<p>Basically I do this:</p>

<pre><code>{% for block in matrix %}

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

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

  {% endif %}
{% endfor %}
</code></pre>

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

<p>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.</p>

<p>So in short, can I safely use cache tags inside other cache tags?</p>

<cache>
Using {% cache globally %} inside {% cache %}
# Using {% cache globally %} inside {% cache %}

(Asked by: username_0 on 2018-08-22)

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?

**Tags:** cache


---

## 1 answers

### Answer by username_1 on 2018-08-24. Score: 2

> 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?](https://craftcms.stackexchange.com/questions/9196/what-effect-does-nesting-cache-tags-have)

[Prevent craftcms caching CMS page with dynamic template](https://craftcms.stackexchange.com/questions/14417/prevent-craftcms-caching-cms-page-with-dynamic-template/14418#14418)


---

