<commit_before># No-Cache

#### A [Craft CMS](http://craftcms.com) Twig extension that escapes caching inside cache blocks

```twig
{% cache %}
	This will be cached
	{% nocache %}
		This won't be
	{% endnocache %}
{% endcache %}
```

It also works when disabling the cache from included files:

```twig
{% cache %}
	This will be cached
	{% include 'template' %}
{% endcache %}
```

_template.twig:_
```twig
{% nocache %}
	This won't be
{% endnocache %}
```

## Example: User information

Here's a common situation. You have a header to your website, and in it you want to a menu of page links, as well as the current logged in user. Since your menu is being generated through database queries, you want to cache your entire header. But you need your user information section to not be cached.

You have two options. One is to segment your cache blocks up, like so:

```twig
{% cache %}
<header>
	<nav>
		<ul>
			{% nav link in craft.entries.section('menu') %}
				<li><a href="{{ link.url }}">{{ link.title }}</a></li>
			{% endnav %}
		</ul>
	</nav>
{% endcache %}
	{% if currentUser %}
		<div>Welcome, {{ currentUser.name }}</div>
	{% endif %}
</header>
```

The problem with this approach is, A) you can't do this if you're including templates with data you don't want to cache, and B) it looks god awful.

Using `{% nocache %}`, we can clean this up quite nicely:

```twig
{% cache %}
<header>
	<nav>
		<ul>
			{% nav link in craft.entries.section('menu') %}
				<li><a href="{{ link.url }}">{{ link.title }}</a></li>
			{% endnav %}
		</ul>
	</nav>
	{% nocache %}
	{% if currentUser %}
		<div>Welcome, {{ currentUser.name }}</div>
	{% endif %}
	{% endnocache %}
</header>
{% endcache %}
```

Ah, that's much better.

## Example: CSRF tokens

A fantastic security feature, but one that basically renders caching impossible to use. Often you might find yourself outputting a form to the frontend, but it's nested deep within a stack of template includes and macros. At the top of this stack you've conveniently wrapped a cache tag around it.

Well, now your CSRF tokens are going to be cached and there's basically nothing you can do about it. Using `{% nocache %}` tags, this is no longer a problem:

```twig
<form>
	{% nocache %}{{ getCsrfInput() }}{% endnocache %}
	...
</form>
```

Now you can include this form anywhere in your templates and not have to worry about your CSRF tokens being cached. As a side note, yes `{% nocache %}` tags _will_ work even when they are not inside a cache block (though try and avoid doing this as `{% nocache %}` tags _do_ add some overhead).

## Caveat

Content inside `{% nocache %}` blocks will render slightly different than normal. Variables declared outside of the `{% nocache %}` block will actually have their values cached for the duration of the cache block.

This causes an issue in situations like the following:

```twig
{% set article = craft.entries.section('news').first %}
{% cache %}
	...
	{% nocache %}
		{{ article.title }}
	{% endnocache %}
{% endcache %}
```<commit_msg>Better example for user information<commit_after># No-Cache

#### A [Craft CMS](http://craftcms.com) Twig extension that escapes caching inside cache blocks

```twig
{% cache %}
	This will be cached
	{% nocache %}
		This won't be
	{% endnocache %}
{% endcache %}
```

It also works when disabling the cache from included files:

```twig
{% cache %}
	This will be cached
	{% include 'template' %}
{% endcache %}
```

_template.twig:_
```twig
{% nocache %}
	This won't be
{% endnocache %}
```

## Example: User information

Say you have a list of products you want to show on your page. Under each product, you want an "add to cart" button. However, you only want to show this button _if_ a user is logged in. Not only that, but you also want to disable the button if the user already has it in their cart. Unfortunately you're outputting 20 products a page with images, so caching the list seems like the responsible thing to do.

```twig
{% cache %}
{% for product in craft.entries.section('products') %}
	<article>
		<figure>{{ product.image.first.img }}</figure>
		<h1>{{ product.title }}</h1>
		{% if currentUser %}
			<button{{ currentUser.cart.id(product.id).total > 0 ? ' disabled' }}>Add to cart</button>
		{% endif %}
	</article>
{% endfor %}
{% cache %}
```

Now we have a problem. The cache around the list of products will cause the `currentUser` logic to essentially not work, since they'll be cached along with the products. You can't isolate the user logic by separating things into multiple cache blocks, since you're in a loop, and the whole point was to cache the database call to grab the product entries. So you either have to apply your user checking in Javascript (far from ideal), or disregard caching altogether.

With `nocache` tags you can fix this very easily:

```twig
{% cache %}
{% for product in craft.entries.section('products') %}
	<article>
		<figure>{{ product.image.first.img }}</figure>
		<h1>{{ product.title }}</h1>
		{% nocache %}
		{% if currentUser %}
			<button{{ currentUser.cart.id(product.id).total > 0 ? ' disabled' }}>Add to cart</button>
		{% endif %}
		{% nocache %}
	</article>
{% endfor %}
{% cache %}
```

The `nocache` block will allow you to cache the entire product list, but still perform your user logic outside of the cache. It also retains context, so you can still refer to products and entries inside the `nocache` block and access their properties, _without_ any additional database calls. This is because the context itself (with all it's variables, macros, etc.) is itself cached. Pretty neat, huh?

## Example: CSRF tokens

A fantastic security feature, but one that basically renders caching impossible to use. Often you might find yourself outputting a form to the frontend, but it's nested deep within a stack of template includes and macros. At the top of this stack you've conveniently wrapped a cache tag around it.

Well, now your CSRF tokens are going to be cached and there's basically nothing you can do about it. Using `{% nocache %}` tags, this is no longer a problem:

```twig
<form>
	{% nocache %}{{ getCsrfInput() }}{% endnocache %}
	...
</form>
```

Now you can include this form anywhere in your templates and not have to worry about your CSRF tokens being cached. As a side note, yes `{% nocache %}` tags _will_ work even when they are not inside a cache block (though try and avoid doing this as `{% nocache %}` tags _do_ add some overhead).

## Caveat

Content inside `{% nocache %}` blocks will render slightly different than normal. Variables declared outside of the `{% nocache %}` block will actually have their values cached for the duration of the cache block.

This causes an issue in situations like the following:

```twig
{% set article = craft.entries.section('news').first %}
{% cache %}
	...
	{% nocache %}
		{{ article.title }}
	{% endnocache %}
{% endcache %}
```
{'fraction_non_alphanumeric': 0.12180974477958237, 'fraction_numerical': 0.001160092807424594, 'mean_word_length': 3.979783393501805, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '996120', 'n_tokens_mistral': 2225, 'n_tokens_neox': 1949, 'n_words': 939}