<p>Unfortunately, the vanilla <code>{% cache %}</code> tag doesn't have a way to escape or ignore certain content.</p>

<p>The obvious workaround is to have more than one <code>{% cache %}</code> tag, where the content that <em>shouldn't</em> be cached sits between/outside those tags.</p>

<p>If you can't do that, another option is to use <em>output buffering</em> and injection. Basically, you cache whatever you need to cache, but <code>{% set %}</code> the output to a variable, rather than rendering it immediately. The important bit: Inside the content that you cache, you put some sort of placeholder value where you want the un-cached content to render. This placeholder can be whatever you want, but make it something that isn't likely to also appear in the actual content (e.g. <code>"%%%foobarbaz%%%"</code>). 
Then, <code>{% set %}</code> the un-cached content to a variable also – and finally, when you print the cached content, you use the <code>|replace</code> filter to replace the placeholder value in the <em>cached</em> content with the variable containing the <em>un-cached</em> content. Something like this:</p>

<pre><code>{# Set the cached content to a variable #}
{% set cachedContent %}
    {% cache globally using key craft.request.path %}

        {# A bunch of taxing queries here #}
        &lt;p&gt;I am content.&lt;/p&gt;

        {# Injection point for un-cached data #}
        %%%foobarbaz%%%

        &lt;p&gt;I am more content.&lt;/p&gt;

    {% endcache %}
{% endset %}

{# Set the uncached content to a variable #}
{% set uncachedContent %}
    &lt;p&gt;I am some content that never is cached wooooo&lt;/p&gt;
{% endset %}

{# Print the whole thing #}
{{ cachedContent|replace('%%%foobarbaz%%%', uncachedContent)|raw }}
</code></pre>

<p>Finally, <a href="https://github.com/benjamminf/craft-nocache" rel="nofollow noreferrer">there's a plugin</a> which may work for you.</p>

CC BY-SA 3.0
2017-04-03T16:40:13.437Z
2017-08-23T20:33:28.647Z
2017-08-23T20:33:28.647Z
Mats Mikkel Rummelhoff
<p>How can I omit a certain tag from being cached?</p>

<p>I have a large page with a fair few queries but halfway down I am loading in a Freeform Form field which cannot be cached. But I'd like all of the content below it to be..</p>

<templating><cache><plugin-freeform>
How to omit certain tag from (% cache %)
# How to omit certain tag from (% cache %)

(Asked by: username_0 on 2017-04-03)

How can I omit a certain tag from being cached?

I have a large page with a fair few queries but halfway down I am loading in a Freeform Form field which cannot be cached. But I'd like all of the content below it to be..

**Tags:** templating, cache, plugin-freeform

---

## 1 answers

### Answer by username_1 on 2017-04-03. Score: 5 | Accepted answer

Unfortunately, the vanilla {% cache %} tag doesn't have a way to escape or ignore certain content.
The obvious workaround is to have more than one {% cache %} tag, where the content that shouldn't be cached sits between/outside those tags.
If you can't do that, another option is to use output buffering and injection. Basically, you cache whatever you need to cache, but {% set %} the output to a variable, rather than rendering it immediately. The important bit: Inside the content that you cache, you put some sort of placeholder value where you want the un-cached content to render. This placeholder can be whatever you want, but make it something that isn't likely to also appear in the actual content (e.g. "%%%foobarbaz%%%"). 
Then, {% set %} the un-cached content to a variable also – and finally, when you print the cached content, you use the |replace filter to replace the placeholder value in the cached content with the variable containing the un-cached content. Something like this:
{# Set the cached content to a variable #}
{% set cachedContent %}
    {% cache globally using key craft.request.path %}

        {# A bunch of taxing queries here #}
        I am content.

        {# Injection point for un-cached data #}
        %%%foobarbaz%%%

        I am more content.

    {% endcache %}
{% endset %}

{# Set the uncached content to a variable #}
{% set uncachedContent %}
    I am some content that never is cached wooooo
{% endset %}

{# Print the whole thing #}
{{ cachedContent|replace('%%%foobarbaz%%%', uncachedContent)|raw }}

Finally, there's a plugin which may work for you.


---

