Default Helpers
Helpers are functions which change or format the data to a form we want. This is summary of the default helpers which are normally available.
All default helpers work with UTF-8 encoded strings.
String modification | |
---|---|
truncate (length, append = '…') |
shortens the length preserving whole words |
substr (offset [, length]) |
returns part of the string |
trim (charset = whitespace) |
strips whitespace or other characters from the beginning and end of the string |
striptags |
removes HTML tags |
strip |
removes whitespace |
webalize (charlist = '…', lower = TRUE) |
returns string in cool URL form |
toAscii |
removes accents |
indent (level = 1, char = "\t") |
indents the text from left with number of tabs |
replace (search, replace = '') |
replaces all occurrences of the search string with the replacement |
replaceRE (pattern, replace = '') |
replaces all occurrences according to regular expression |
padLeft (length, pad = ' ') |
completes the string to given length from left |
padRight (length, pad = ' ') |
completes the string to given length from right |
repeat (count) |
repeats the string |
implode (glue = '') |
joins an array to a string |
nl2br |
new lines with <br> |
Letter casing | |
lower |
makes a string lower case |
upper |
makes a string upper case |
firstUpper |
makes the first letter upper case |
capitalize |
lower case, the first letter of each word upper case |
Formatting | |
date (format) |
formats date |
number (decimals = 0, decPoint = '.') |
formats number |
bytes (precision = 2) |
formats size in bytes |
Other | |
noescape |
prints a variable without escaping |
dataStream (mimetype = detect) |
Data URI protocol conversion |
url or escapeurl |
escapes parameter in URL |
length |
returns length of a string |
null |
flushes the input, returns nothing |
Truncate
Shortens a string to the maximum given length but tries to preserve whole words. If the string is truncated it adds ellipsis at the end (this can be changed by the second parameter).
{var $title = 'Hello, how are you?'}
{$title|truncate:5} Hell…
{$title|truncate:17} Hello, how are…
{$title|truncate:30} Hello, how are you?
Strip
Removes unnecessary whitespace from the output.
{block |strip}
<ul>
<li>Hello</li>
</ul>
{/block}
Generates
<ul> <li>Hello</li> </ul>
Webalize
Transforms a string in the form suitable for URL, ie. removes accents and replaces all characters with dash except English alphabet letters and numbers.
{var $name = 'Česká republika'}
{$name|webalize} ceska-republika
Indent
Indents a text from left by a given number of tabs or other characters which we specify in the second optional argument:
<div>
{block |indent}
<p>Hello</p>
{/block}
</div>
Generates
<div>
<p>Hello</p>
</div>
Letter casing
{var $s = 'blue COLOR'}
{$s|lower} blue color
{$s|upper} BLUE COLOR
{$s|firstUpper} Blue COLOR
{$s|capitalize} Blue Color
Date
Returns a date in the given format using options of strftime or date PHP functions. Helper gets a date as a UNIX timestamp, a string or an object of
DateTime
type.
{$today|date:'%d.%m.%Y'}
Number
Formats a number to given number of decimal places. You can also specify a character of the decimal point and thousands separator.
{1234.20 |number} 1,234
{1234.20 |number:1} 1,234.2
{1234.20 |number:2} 1,234.20
{1234.20 |number:2:',':' '} 1 234,20
Bytes
Formats a size in bytes to human-readable form.
{$size|bytes} 0 B, 10 B or 1.25 GB, …
DataStream
Converts the data to URI scheme. With this helper you can insert images to HTML or CSS without linking external files. All the modern browsers and Internet Explorer since version 8 support it.
Lets have an image in a variable $img = Image::fromFile('obrazek.gif')
, then
<img src="{$img|dataStream}">
generates for example:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==">
Url
Escapes a variable to be used as a parameter in URL.
<a href="http://example.com/{$name|url}">{$name}</a>
or
<a href="http://example.com/{$name|escapeurl}">{$name}</a>