Monday, January 18, 2010

Smarty : Variable Modifiers

Here list of all Smarty Variable Modifiers
  • capitalize
  • cat
  • count_characters
  • count_paragraphs
  • count_sentences
  • count_words
  • date_format
  • default
  • escape
  • indent
  • lower
  • nl2br
  • regex_replace
  • replace
  • spacify
  • string_format
  • strip
  • strip_tags
  • truncate
  • 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 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.

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}
{* php's shuffle on servers's ip *}
{$smarty.server.SERVER_ADDR|shuffle}
(* this will uppercase and truncate the whole array *}
<select name="name_id">
{html_options output=$myArray|upper|truncate:20}
</select>
  • If you apply a modifier to an array variable instead of a single value variable, the modifier will be applied to every value in that array. If you really want the modifier to work on an entire array as a value, you must prepend the modifier name with a @ symbol.
    Example: {$articleTitle|@count} - will print out the number of elements in the $articleTitle array using the php count() [http://php.net/count] function as a modifier.
  • Modifiers are autoloaded from the $plugins_dir or can be registered explicitly with the register_modifier() 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 phpfunctions 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 MODIFIER_FUNCS element of the $security_settings array.
See also register_modifier(), 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 ucfirst() [http://php.net/ucfirst] function.
Parameter Position : 1
Type : boolean
Required : no
Default : FALSE
Description : This determines whether or not words with digits will be uppercased
Example : capitalize
<?
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.

cat

This value is concatenated to the given variable.

Parameter Position : 1
Type : string
Required : No
Default : empty
Description : This value to catenate to the given variable.

Example : cat
<?php
$smarty->assign('articleTitle', "Psychics predict world didn't end");
?>
Where template is:
{$articleTitle|cat:' yesterday.'}
Will output:
Psychics predict world didn't end yesterday.

No comments:

Post a Comment