Nested Passes - {code} and {page}

A nested pass is the engine run inside itself: the content of a {code} pair, or the page a {page} tag names, is rendered as a complete PAD run of its own, and what it produced lands in the surrounding page. The pass sees the request it runs in - its variables, its stores - unless it asks for isolation.

A pass and what it leaves behind

A plain {code} shares everything. What the pass creates is still there when it returns:

PHP
HTML
fragments/page_1.pad
Result
{code}
{set $made = 'and left this behind'/}
The pass ran
{/code}
<p>After it: {$made}</p>
The pass ran

After it: and left this behind

The four isolation forms

Three options decide how much of the request a pass sees, and how much of the pass the request keeps. They differ on both sides of the boundary:

form sees the outer state what it created afterwards
{code} yes still there
{code reset} no - a blank slate going in still there
{code clean} yes taken back out
{code sandbox} no taken back out

 

PHP
HTML
fragments/page_2.pad
Result
<p>plain: [{code}{set $f1 = 'x'/}{/code}{$f1 | optional}]</p>
<p>reset: [{code reset}{set $f2 = 'x'/}{/code}{$f2 | optional}]</p>
<p>clean: [{code clean}{set $f3 = 'x'/}{/code}{$f3 | optional}]</p>
<p>sandbox: [{code sandbox}{set $f4 = 'x'/}{/code}{$f4 | optional}]</p>

plain: [x]

reset: [x]

clean: []

sandbox: []

Another page as a pass

{page 'name'} runs that page - its .php and its .pad - as a pass, with the same isolation options. A function option runs the pass inside a PHP function body, but it still starts from the caller's variables and what it sets comes back out - a page whose .php half must not touch the caller's variables wants clean or sandbox:

PHP
HTML
fragments/page_3.pad
Result
{page 'hello/hello'}

Hello World !

Stores cross the boundary by the same rules

The data, content, bool and sequence stores follow the isolation form. A clean pass reads the request's stores; a sandbox or reset pass starts without them. On the way out, only a plain pass's pushes stay: each of the three isolated forms puts the request's stores back exactly as they were, its own pushes included:

PHP
HTML
fragments/page_4.pad
Result
{data 'fruit'}["apple","pear"]{/data}
<p>A clean pass sees the store: {code clean}{fruit}{$fruit} {/fruit}{/code}</p>
{code}{sequence '7;8;9', push='nums'/}{/code}
<p>A plain pass pushed nums, and it survived: {pull:nums}{$sequence} {/pull:nums}</p>

A clean pass sees the store: apple pear

A plain pass pushed nums, and it survived: 7 8 9

Engine state is always put back

Whatever the isolation form, the engine itself returns to where it was: a loop the pass ran inside continues from the same row, and a request-lived counter such as {switch}'s rotation is rolled back with the rest - a pass takes its counting with it:

PHP
HTML
fragments/page_5.pad
Result
<p>Inside a pass: {code}{switch 'first', 'second'}{/code}</p>
<p>After it: {switch 'first', 'second'}</p>

Inside a pass: first

After it: first

Both calls answer the first value: the rotation the pass advanced was restored when the pass returned.