{code}

Runs its content as PAD source in a separate engine pass and outputs what that pass produced.

Pages and execution Tag pairs

Syntax

{code} ... {/code}
{code sandbox} ... {/code}
{code reset} ... {/code}
{code clean} ... {/code}
{code function='name'} ... {/code}
{echo $snippet | code}

Options

sandbox
Bare option: the pass sees none of the page's variables and stores and leaves none behind - what {sandbox} does.
reset
Bare option: the level arrays and the data, content, bool and sequence stores are cleared for the pass and restored after it; variables stay visible, and what the pass sets stays behind.
clean
Bare option: the pass sees everything around it, but every variable it creates or changes is undone afterwards.
function
Runs the pass inside a PHP function of that name, giving it a variable scope of its own; what it creates is copied out afterwards (not with sandbox).

Description

{code}...{/code} takes its content as PAD source and renders it in a separate engine pass - a fresh build, as if the content were a little page of its own - and outputs what that pass produced. Inside, everything a page can do works: tags, fields, expressions, {set}.

Its main use is as a pipe. A value is text in PAD: a field holding {echo 1 + 1} prints those characters, it does not run them. Piping the value into code - {echo $snippet | code} - is the explicit way to say that it is a template, for a snippet kept in a database or a file.

By default the pass shares the page's state: it sees the variables and the stores, and what it sets stays behind. Four options change how isolated it is:

  • sandbox - nothing in, nothing out; the same as the {sandbox} tag and the sandbox pipe;
  • reset - the levels and the stores start empty and are put back afterwards, while variables are shared as in a plain pass;
  • clean - the pass sees everything, and what it created or changed is undone when it ends;
  • function='name' - the pass runs inside a PHP function, so variables get a scope of their own; what it creates is copied out unless sandbox is given too.

The same four options are read by {page}, which runs a whole page in such a pass. To show braces without running them, the opposite tool is {ignore}.

Examples

Content run as a pass

The content is rendered in a separate engine pass and its output takes the tag's place.

Templateexamples/code/1-run.pad
<p>{code}Six times seven is {echo 6 * 7}.{/code}</p>
Resultopen alone

Six times seven is 42.

A value run as PAD

A value is text; piped into code it runs as a template.

Templateexamples/code/2-value.pad
<p>As text: {$snippet}</p>
<p>Run: {echo $snippet | code}</p>
PHPexamples/code/2-value.php
<?php

  $snippet = 'The total is {echo 19 + 23}.';

?>
Resultopen alone

As text: The total is {echo 19 + 23}.

Run: The total is 42.

plain, reset, clean and sandbox

Whether a variable set inside the pass is still there after it.

Templateexamples/code/3-isolation.pad
<p>plain: [{code}{set $made1 = 'kept'/}{/code}{$made1 | optional}]</p>
<p>reset: [{code reset}{set $made2 = 'kept'/}{/code}{$made2 | optional}]</p>
<p>clean: [{code clean}{set $made3 = 'kept'/}{/code}{$made3 | optional}]</p>
<p>sandbox: [{code sandbox}{set $made4 = 'kept'/}{/code}{$made4 | optional}]</p>
Resultopen alone

plain: [kept]

reset: [kept]

clean: []

sandbox: []

See also

  • {sandbox} Runs its content as PAD source in an isolated pass that cannot see or leave behind anything of the page.
  • {page} Renders another page of the application - its PHP and its template - in place of the tag.
  • {ignore} Keeps PAD from reading the braces in its content as tags - for inline JavaScript, CSS, JSON and code samples.
  • {pad} A tag with no behaviour of its own: its content renders once, or per row with data=, carrying the generic options.