{push}
Renders its content and adds it to a named stack, for a stack tag elsewhere in the page to print.
Syntax
{push 'name'} ... {/push}
{push 'name', once='key'} ... {/push}
{push 'name', once} ... {/push}
Parameters
-
name - The name of the stack, required.
Options
-
once - With a key: the first push of that key to this stack is kept, later ones are skipped before their content renders. Bare: a push whose rendered text the stack already holds is dropped.
Description
{push 'name'} renders its content where it stands - with the variables of that spot - prints nothing there, and adds the rendering to the stack of that name. A {stack 'name'} anywhere in the page prints everything pushed to it, in the order it was pushed. This is how a component deep in a page brings the script or stylesheet it needs into the <head> of the wrapper. Like {tidy} it runs twice: first to ask for the end walk, then with the rendered content.
once='key' keeps only the first push of that key to the stack: a later one is skipped before its content even renders - for a component used ten times that needs its script once. once without a key compares the rendered text instead and drops a push whose text the stack already holds.
A push inside a {cache} section is stored with the section and made again on every hit, so a cached component still brings its script. A plain {page} or {code} pass adds to the page's stacks; a sandbox, clean or reset pass keeps its pushes to itself. From PHP: padStackPush ( 'scripts', $html, $once ).
Under the strict check a push without a stack name, and a push that never closes, are errors.
Examples
Pushes from a loop
Each row pushes a line to a stack printed above the loop; the stack is filled in after the page rendered.
<p>Cities with a delivery today:</p>
<ul>{stack 'pushDelivery'}</ul>
{deliveries}
{if $today}{push 'pushDelivery'}<li>{$city}</li>{/push}{/if}
{/deliveries}
<?php
$deliveries = [
[ 'city' => 'Utrecht', 'today' => TRUE ],
[ 'city' => 'Antwerp', 'today' => FALSE ],
[ 'city' => 'Rotterdam', 'today' => TRUE ],
[ 'city' => 'Ghent', 'today' => TRUE ] ];
?>
Cities with a delivery today:
- Utrecht
- Rotterdam
- Ghent
Once per key
Three uses of a component each push its script line; once= keeps only the first, the bare once drops a repeated text.
{sequence 3, name='use'}
{push 'pushScripts', once='chart'}<li>script src="chart.js" (pushed by use {$use})</li>{/push}
{push 'pushScripts', once}<li>link href="chart.css"</li>{/push}
{/sequence}
<ul>{stack 'pushScripts'}</ul>
- script src="chart.js" (pushed by use 1)
- link href="chart.css"