Pipes - Transform Output with Functions
Pipes allow you to pass output through functions to transform it. Use the pipe symbol | followed by a function name to process data.
Two Ways to Use Pipes
1. Variable Pipes
Apply functions to a variable with {$variable | function} - the field tag runs the sanitize chain on the result - or with {echo $variable | function}, which prints it raw, as the examples on this page do:
|
PHP fragments/pipes_1.php |
HTML fragments/pipes_1.pad |
Result |
|---|---|---|
|
|
Original: john doe Uppercase: JOHN DOE Capitalize: John Doe Original: ALICE@EXAMPLE.COM Lowercase: alice@example.com Original: 1234.56 Formatted: 1,234.56 |
2. Tag Pipes
Apply functions to tag output. Pipes can be placed on the opening tag or closing tag:
|
PHP fragments/pipes_2.php |
HTML fragments/pipes_2.pad |
Result |
|---|---|---|
|
|
Items separated by commas: apple, banana, cherry Items in uppercase:
|
Pipe Timing: Opening vs Closing Tag
The placement of the pipe determines when the function is applied:
-
Opening tag
{tag | function}- transforms the content template, once, before any occurrence is rendered. What the function is handed is the source between the tags, not the tag's data. -
Closing tag
{/tag | function}- transforms the output, after every occurrence has been rendered and joined.
An opening pipe is therefore not a way to reorder or filter what a tag iterates: a field written inside the content would be transformed along with everything else, and then not resolve. Reshaping the data is what the data handling options are for.
|
PHP |
HTML fragments/pipes_4.pad |
Result |
|---|---|---|
|
Closing tag pipe - handed everything the tag rendered: [ALPHA BETA ] Opening tag pipe - handed the content template, which then repeats: [ROW ROW ] |
Chaining Pipes
Chain multiple pipe functions together by separating them with |. Functions are applied left to right:
|
PHP fragments/pipes_3.php |
HTML fragments/pipes_3.pad |
Result |
|---|---|---|
|
|
Chain multiple pipes together: Original: [ hello world ] Trimmed and uppercase: [HELLO WORLD] Original: the quick brown fox Uppercase first letter of each word: The Quick Brown Fox Replace and capitalize each word: The Quick Brown Dog |
Presentation helpers
truncate shortens on a whole word, slug makes a readable URL part, bytes writes a size for people, and escape writes a value for the context it lands in - html, attr, js, css or url:
|
PHP fragments/pipes_5.php |
HTML fragments/pipes_5.pad |
Result |
|---|---|---|
|
|
The quick brown fox jumps… ?page=creme-brulee-co 3 GB it\x27s\x20\x3C\x2Fscript\x3E |
Common Pipe Functions
| Function | Description | Example |
|---|---|---|
upper
|
Convert to uppercase |
{echo $text | upper}
|
lower
|
Convert to lowercase |
{echo $text | lower}
|
ucwords
|
Capitalize each word |
{echo $text | ucwords}
|
trim
|
Remove whitespace |
{echo $text | trim}
|
html
|
HTML encode |
{echo $text | html}
|
replace(a,b)
|
Replace text |
{echo $text | replace('old','new')}
|
+ n
|
Add number |
{echo $count | + 1}
|
date(fmt)
|
Format date |
{echo $timestamp | date('Y-m-d')}
|
Important Rules
-
A literal needs {echo} -
{echo 'text' | upper}; a field pipes as it stands,{$var | upper}, as well as through{echo $var | upper}- which prints the value raw, without the sanitize chain the field tag runs -
Arithmetic needs spaces -
{echo $x | + 1}NOT{echo $x | +1} -
Quote string parameters -
{echo $x | replace('a','b')} -
Functions apply left to right -
{echo $x | trim | upper}trims first, then uppercases
When to Use Opening vs Closing Tag Pipes
| Use Case | Placement | Example |
|---|---|---|
| Transform the content template before it repeats | Opening tag |
{items | trim}...{/items}
|
| Reshape the data itself | Option, not a pipe |
{items sort}...{/items}
|
| Transform final output after rendering | Closing tag |
{tag}...{/tag | upper}
|
| Format variable values | Variable pipe |
{echo $price | number_format(@, 2)}
|
See Also
- {echo} - Evaluate and output expressions
- {functions} - List of all pipe functions
- {options} - Tag options reference