{echo}

Evaluates an expression and prints the result - the general output tag, and the one to put in front of a pipe.

Output control Single tags

Syntax

{echo expression}
{echo expression | pipe | pipe}

Parameters

expression
A PAD expression: fields, literals, arithmetic, comparisons, a ternary - and pipes after it. The whole text up to the closing brace is the expression.

Description

{echo} evaluates the expression written after its name and prints the value. The whole text up to the closing brace is one expression - it is not split into parameters - so {echo $price * $qty}, {echo $n gt 1 ? 'items' : 'item'} and {echo 'Hello ' . $name} all work. A quoted text is a literal: {echo 5 + 3} prints 8, {echo '5 + 3'} prints the text itself.

It is the tag to put in front of a pipe when the value is a literal or an expression: {echo $text | trim | upper}, {echo $value | + 1} (an arithmetic pipe needs the space after its operator). A field can be piped directly as well - {$name | upper} - and that is the difference that matters: the field tag runs the sanitize chain, {echo} does not. {echo $html} writes the value as it stands, markup and all, so use {$html} for anything a visitor typed and keep {echo} for values the application made itself.

A value is text, never template code: with $v = '{php:getcwd}', {echo $v} prints those characters rather than running them. TRUE prints as 1, FALSE and NULL as nothing.

Under the strict syntax check an {echo} with nothing before its first pipe - or nothing at all - is an error, as is an undefined field or a pipe function that does not exist.

Examples

Expressions

Arithmetic, a joined text and a ternary - and a quoted text that stays text.

Templateexamples/echo/1-expression.pad
{set $price = 4.5}
{set $qty = 3}
<p>Total: {echo $price * $qty}</p>
<p>{echo $qty . ' ' . ( $qty gt 1 ? 'tickets' : 'ticket' )}</p>
<p>{echo 5 + 3} versus {echo '5 + 3'}</p>
Resultopen alone

Total: 13.5

3 tickets

8 versus 5 + 3

In front of a pipe

A literal or an expression goes through pipe functions; arithmetic pipes need a space after the operator.

Templateexamples/echo/2-pipes.pad
<p>{echo '  hello world  ' | trim | upper}</p>
<p>{echo 20 | + 1 | * 2}</p>
<p>{echo 'herbert@example.com' | after('@') | before('.')}</p>
Resultopen alone

HELLO WORLD

42

example

Raw against sanitized

The field tag escapes the markup of a value; echo writes it as it stands.

Templateexamples/echo/3-raw.pad
{set $note = '<em>fresh</em> & hot'}
<p>Field tag: {$note}</p>
<p>echo: {echo $note}</p>
Resultopen alone

Field tag: <em>fresh</em> & hot

echo: fresh & hot

See also

  • {set} Assigns one or more variables, as globals that outlive the level the tag stands in.
  • {field} Runs a SQL query on the application database and prints the single value it answers.
  • {ignore} Keeps PAD from reading the braces in its content as tags - for inline JavaScript, CSS, JSON and code samples.
  • {trans} Writes the text a key has in the _lang/ catalog of the request's locale, with plural forms and substitutions.