Smarty uses modifiers to display text in different ways, for example to display as a string in capital letters, small letters, or title case. Any variable in a template can have a modifier applied to it by following it with a pipe symbol ('|
') and the name of a modifier:
{$user.userName} |
Displays the variable $user.userName without any change in formatting
|
{$user.userName|upper} |
Displays the variable $user.userName in upper case letters using the upper modifier
|
Several modifiers can be chained together by separating each with a pipe symbol:
{$user.userName|upper|strip_tags} |
Displays the variable $user.userName applying the upper modifier first followed by the strip_tags modifier
|
Certain modifiers can take parameters to control their behaviour, for example the built-in Smarty modifier truncate
which truncates a string to a given length. The desired length of the string is passed as a parameter to the modifier:
{$user.userName|truncate:20} |
Displays the variable $user.userName truncated to 20 characters in length
|
If a modifier has several parameters, they must be passed in sequences, separated by colons. Optional parameters that you do not wish to specify a value for are given by omitting any value after its colon:
{$user.userName|truncate:20::true:true} |
Displays the variable $user.userName and passes four parameters to the truncate modifier:
|
Further information on using modifiers in templates, and the built-in modifiers in Smarty, can be found in the Smarty manual.
Gallery2 adds some extra modifiers to Smarty that help to format text for use in a gallery. These are:
Usage: entitytruncate
:length:etc:breakWords
The entitytruncate
modifier truncates a string, but avoids making the truncation in the middle of an HTML entity or a multibyte character
Parameters:
length
(integer)- maximum length for output stringetc
(string) - a string to append to the end of a truncated string; the default is three full-stops ('...')breakWords
(boolean) - if true, entitytruncate
will break a string in the middle of a word. The default is false
Usage: ireplace
:search:replace
ireplace
performs a case insensitive search and replace on the variable it modifies, replacing search
with replace
Parameters:
search
(string) - a string to find in the variablereplace
(string) - a string to replace search
withUsage: markup
The markup
modifier formats a variable according to the markup format set in the Site Admin settings (raw HTML, BBCode, or no formatting)
Usage: print_r
This modifier is used for debugging purposes and prints a variable recursively if it contains subkeys. More information on the print_r
keyword is available in the PHP Manual
Usage: repeat
:count
The repeat
modifier repeats the variable a given number of times
Parameters:
count
(integer) - the number of times to repeat the variable that this repeat
is modifyingUsage: split
:separator:regexp
split
splits a variable into an array using the separator given. Further information on split
is available in the PHP Manual
Parameters:
separator
(string) - the string that marks the points where the variable should be splitregexp
(boolean) - if true
then separator
is interpreted as a regular expression. The default is false