|
<h1>Pipes - Transform Output with Functions</h1>
<p>Pipes allow you to pass output through functions to transform it. Use the pipe symbol <code>|</code> followed by a function name to process data.</p>
<h2>Two Ways to Use Pipes</h2>
<h3>1. Variable Pipes</h3>
<p>Apply functions to variables using <code>{echo $variable | function}</code>:</p>
{example 'fragments/pipes_1'}
<h3>2. Tag Pipes</h3>
<p>Apply functions to tag output. Pipes can be placed on the opening tag or closing tag:</p>
{example 'fragments/pipes_2'}
<h2>Pipe Timing: Opening vs Closing Tag</h2>
<p>The placement of the pipe determines <strong>when</strong> the function is applied:</p>
<ul> <li><strong>Opening tag</strong> <code>{tag | function}</code> - Processes data BEFORE the tag content is rendered</li> <li><strong>Closing tag</strong> <code>{/tag | function}</code> - Processes output AFTER the tag is finished</li> </ul>
{example 'fragments/pipes_4'}
<h2>Chaining Pipes</h2>
<p>Chain multiple pipe functions together by separating them with <code>|</code>. Functions are applied left to right:</p>
{example 'fragments/pipes_3'}
<h2>Common Pipe Functions</h2>
<table border="1" cellpadding="10" cellspacing="0" style="border-collapse: collapse; width: 100%;"> <tr style="background: #f0f0f0;"> <th>Function</th> <th>Description</th> <th>Example</th> </tr> <tr> <td><code>upper</code></td> <td>Convert to uppercase</td> <td><code>{echo $text | upper}</code></td> </tr> <tr> <td><code>lower</code></td> <td>Convert to lowercase</td> <td><code>{echo $text | lower}</code></td> </tr> <tr> <td><code>ucwords</code></td> <td>Capitalize each word</td> <td><code>{echo $text | ucwords}</code></td> </tr> <tr> <td><code>trim</code></td> <td>Remove whitespace</td> <td><code>{echo $text | trim}</code></td> </tr> <tr> <td><code>html</code></td> <td>HTML encode</td> <td><code>{echo $text | html}</code></td> </tr> <tr> <td><code>replace(a,b)</code></td> <td>Replace text</td> <td><code>{echo $text | replace('old','new')}</code></td> </tr> <tr> <td><code>+ n</code></td> <td>Add number</td> <td><code>{echo $count | + 1}</code></td> </tr> <tr> <td><code>date(fmt)</code></td> <td>Format date</td> <td><code>{echo $timestamp | date('Y-m-d')}</code></td> </tr> </table>
<h2>Important Rules</h2>
<ul> <li><strong>Use {tag 'echo'} with variables</strong> - <code>{echo $var | function}</code> NOT <code>{$var | function}</code></li> <li><strong>Arithmetic needs spaces</strong> - <code>{echo $x | + 1}</code> NOT <code>{echo $x | +1}</code></li> <li><strong>Quote string parameters</strong> - <code>{echo $x | replace('a','b')}</code></li> <li><strong>Functions apply left to right</strong> - <code>{echo $x | trim | upper}</code> trims first, then uppercases</li> </ul>
<h2>When to Use Opening vs Closing Tag Pipes</h2>
<table border="1" cellpadding="10" cellspacing="0" style="border-collapse: collapse; width: 100%;"> <tr style="background: #f0f0f0;"> <th>Use Case</th> <th>Placement</th> <th>Example</th> </tr> <tr> <td>Transform input data before processing</td> <td>Opening tag</td> <td><code>{items | sort}...{/items}</code></td> </tr> <tr> <td>Transform final output after rendering</td> <td>Closing tag</td> <td><code>{tag}...{/tag | upper}</code></td> </tr> <tr> <td>Format variable values</td> <td>Variable pipe</td> <td><code>{echo $price | number_format(2)}</code></td> </tr> </table>
<h2>See Also</h2>
<ul> <li>{tag 'echo'} - Evaluate and output expressions</li> <li>{tag 'functions'} - List of all pipe functions</li> <li>{tag 'options'} - Tag options reference</li> </ul>
|
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 variables using {echo $variable | function}:
2. Tag Pipes
Apply functions to tag output. Pipes can be placed on the opening tag or closing tag:
Pipe Timing: Opening vs Closing Tag
The placement of the pipe determines when the function is applied:
- Opening tag
{tag | function} - Processes data BEFORE the tag content is rendered
- Closing tag
{/tag | function} - Processes output AFTER the tag is finished
Chaining Pipes
Chain multiple pipe functions together by separating them with |. Functions are applied left to right:
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
- Use {echo} with variables -
{echo $var | function} NOT {$var | function}
- 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 input data before processing |
Opening tag |
{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
|