{nocache}

Marks a part of a cached page or {cache} section that renders again on every request, the hit included.

Output control Tag pairs

Syntax

{nocache} ... {/nocache}
{cache 'name', ttl=300} ... {nocache} ... {/nocache} ... {/cache}

Description

Where nothing caches, {nocache} is invisible: its content renders as any content does. It matters inside a {cache} section (or a tag's cache= option) that is rendering, and on a page the page cache may keep. There the part is stored as its source rather than as its output; a hit hands back the stored rendering with each part rendered again for this request.

A part inside a loop gets the fields of its rows back as they were when the section was stored, so {topProducts}...{nocache}{stock $id}{/nocache}... looks up the live stock of each cached product. In a section the part renders where the section stands, with the page's fields. A variable or store the cached text made before the part is not there on a hit - a section keeps its text, not its effects - so what a part reads is made before the section or in the part itself.

A page-cache hit runs no PHP - not the page's, nor an _inits.php - so a part there sees what a request has without it: request and session values, the configuration, the _lib functions and the application's tags. A page-cache entry with parts is never answered with a 304 from the entry alone, nor does a hit renew it.

A {form}'s CSRF token or a {nonce} inside a part no longer keeps a section out of the fragment cache: the part renders for each visitor. {nocache} takes no parameter, and is always a pair - a single {nocache} is an error under the strict check.

Examples

A live part in a cached section

The list is kept five minutes; the greeting renders on every request - try ?examples/nocache/1-section&visitor=Ann.

Templateexamples/nocache/1-section.pad
{cache 'nocacheDemo', ttl=300}
  <p>{nocache}Hello, {$nocacheVisitor}!{/nocache}</p>
  <ul>
  {nocacheProducts}
    <li>{$name}</li>
  {/nocacheProducts}
  </ul>
{/cache}
PHPexamples/nocache/1-section.php
<?php

  $nocacheVisitor  = padRequest ( 'visitor', 'guest' );

  $nocacheProducts = [
    [ 'name' => 'Kettle'  ],
    [ 'name' => 'Toaster' ],
    [ 'name' => 'Blender' ] ];

?>
Resultopen alone

Hello, guest!

  • Kettle
  • Toaster
  • Blender

Per row of a cached loop

Each part gets the fields of its row back, so the stock is looked up live for every cached product.

Templateexamples/nocache/2-rows.src
{cache 'top-products', ttl=300}
  <ul>
  {topProducts}
    <li>{$name} {nocache}- {stock $id} left{/nocache}</li>
  {/topProducts}
  </ul>
{/cache}

In a page the page cache keeps

With $padCache on, the page is served from the cache; the time is rendered again on every hit, without running any PHP.

Templateexamples/nocache/3-page-cache.src
<h1>Our products</h1>
{products}<p>{$name}</p>{/products}

<p>Served at {nocache}{php:date 'H:i:s'}{/nocache}</p>

See also

  • {cache} Keeps a named section of the page rendered for a number of seconds - the fragment cache.
  • {fragment} A named part of the page that a request can ask for alone - for htmx and ajax swaps.
  • {csrf} The hidden form field holding the CSRF token of the visitor's session, or the bare token.
  • {nonce} This request's Content-Security-Policy nonce, for a script or style the policy lets run.