Posted by: prajapatinilesh | February 12, 2008

Variable Modifiers in smarty

index.php

include(‘Smarty.class.php’);// create object
$smarty = new Smarty;
// assign some content. This would typically come from
// a database or other source, but we’ll use static
// values for the purpose of this example.
$smarty->assign(‘name’, ‘george smith’);
$smarty->assign(‘address’, ’45th & Harris’);
// display it
$smarty->display(‘index.tpl’);

index.tpl

<html>
<head>
<title>User Info</title>
</head>
<body>User Information:<p>
Name: {$name}<br>
Address: {$address}<br>
</body>
</html>

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
 
Example:

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

Note:
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).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: