PhpRiot
Follow phpriot on Twitter
Sponsored Link
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

Variable Modifiers

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 modifer 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 29. 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="smarty@example.com"}

{* 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>

  

  • Modifiers can be applied to any type of variables, including arrays and objects.

    Note

    The default behavior was changed with Smarty 3. In Smarty 2.x, you had to use an "@" symbol to apply a modifier to an array, such as {$articleTitle|@count}. With Smarty 3, the "@" is no longer necessary, and is ignored.

    If you want a modifier to apply to each individual item of an array, you will either need to loop the array in the template, or provide for this functionality inside your modifier function.

  • Modifiers are autoloaded from the $plugins_dir or can be registered explicitly with the registerPlugin() function. The later is useful for sharing a function between php scripts and smarty templates.

  • All php-functions can be used as modifiers implicitly, as demonstrated in the example above. However, using php-functions as modifiers has two little pitfalls:

    • First - sometimes the order of the function-parameters is not the desirable one. Formatting $foo with {"%2.f"|sprintf:$foo} actually works, but asks for the more intuitive, like {$foo|string_format:"%2.f"} that is provided by the Smarty distribution.

    • Secondly - if security is enabled, all php-functions that are to be used as modifiers have to be declared trusted in the $modifiers property of the securty policy. See the Security section for details.

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

Smarty Template Engine