Chapter 5. Variable Modifiers

Table of Contents

capitalize
cat
count_characters
count_paragraphs
count_sentences
count_words
date_format
default
escape
from_charset
indent
lower
nl2br
regex_replace
replace
spacify
string_format
strip
strip_tags
to_charset
truncate
unescape
upper
wordwrap

Variable modifiers can be applied to variables, custom functions or strings. To apply a modifier, specify the value followed by a | (pipe) and the modifier name. A modifier may accept additional parameters that affect its behavior. These parameters follow the modifier name and are separated by a : (colon). Also, all php-functions can be used as modifiers implicitly (more below) and modifiers can be combined.

Example 5.1. Modifier examples

{* apply modifier to a variable *}
{$title|upper}

{* modifier with parameters *}
{$title|truncate:40:"..."}

{* apply modifier to a function parameter *}
{html_table loop=$myvar|upper}

{* with parameters *}
{html_table loop=$myvar|truncate:40:"..."}

{* apply modifier to literal string *}
{"foobar"|upper}

{* using date_format to format the current date *}
{$smarty.now|date_format:"%Y/%m/%d"}

{* apply modifier to a custom function *}
{mailto|upper address="[email protected]"}

{* using  php's str_repeat *}
{"="|str_repeat:80}

{* php's count *}
{$myArray|@count}

{* this will uppercase and truncate the whole array *}
<select name="name_id">
{html_options output=$my_array|upper|truncate:20}
</select>

  

See also registerPlugin(), combining modifiers. and extending smarty with plugins

capitalize

This is used to capitalize the first letter of all words in a variable. This is similar to the PHP ucwords() function.

Parameter Position Type Required Default Description
1 boolean No FALSE This determines whether or not words with digits will be uppercased
2 boolean No FALSE This determines whether or not Capital letters within words should be lowercased, e.g. "aAa" to "Aaa"

Example 5.2. capitalize

<?php

$smarty->assign('articleTitle', 'next x-men film, x3, delayed.');

?>

   

Where the template is:

{$articleTitle}
{$articleTitle|capitalize}
{$articleTitle|capitalize:true}

   

Will output:

next x-men film, x3, delayed.
Next X-Men Film, x3, Delayed.
Next X-Men Film, X3, Delayed.

   

See also lower and upper