Variable modifiers can be applied to any variable to alter its contents. To
apply a modifier, specify the variable followed by the |
(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 : (colon).
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 an @ symbol like so:
{$articleTitle|@count} (this will print out the number of
elements in the $articleTitle array.)
This is used to capitalize the first letter of all words in a variable.
Example 3-12. capitalize {$articleTitle}
{$articleTitle|capitalize}
OUTPUT:
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers. |
|
This is used to count the number of characters in a variable.
Example 3-13. count_characters
{$articleTitle}
{$articleTitle|count_characters}
OUTPUT:
Cold Wave Linked to Temperatures
32 |
|
This is used to count the number of paragraphs in a variable.
Example 3-14. count_paragraphs
{$articleTitle}
{$articleTitle|count_paragraphs}
OUTPUT:
War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.
Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.
2 |
|
This is used to count the number of sentences in a variable.
Example 3-15. count_sentences
{$articleTitle}
{$articleTitle|count_sentences}
OUTPUT:
Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.
2 |
|
This is used to count the number of words in a variable.
Example 3-16. count_words
{$articleTitle}
{$articleTitle|count_words}
OUTPUT:
Dealers Will Hear Car Talk at Noon.
7 |
|
This formats a date and time into the given strftime() format.
Dates can be passed to Smarty as unix timestamps, mysql timestamps
or any string made up of month day year (parsable by strtotime).
Designers can then use date_format to have complete control of the
formatting of the date.
Example 3-17. date_format {$currentDate}
{$currentDate|date_format:"%A, %B %e, %Y"}
{$currentDate|date_format:"%H:%M:%S"}
OUTPUT:
Feb 6, 2001
Tuesday, February 6, 2001
14:33:00 |
|
Example 3-18. date_format conversion specifiers %a - abbreviated weekday name according to the current locale
%A - full weekday name according to the current locale
%b - abbreviated month name according to the current locale
%B - full month name according to the current locale
%c - preferred date and time representation for the current locale
%C - century number (the year divided by 100 and truncated to an integer, range 00 to 99)
%d - day of the month as a decimal number (range 00 to 31)
%D - same as %m/%d/%y
%e - day of the month as a decimal number, a single digit is preceded by a
space (range 1 to 31)
%g - Week-based year within century [00,99]
%G - Week-based year, including the century [0000,9999]
%h - same as %b
%H - hour as a decimal number using a 24-hour clock (range 00 to 23)
%I - hour as a decimal number using a 12-hour clock (range 01 to 12)
%j - day of the year as a decimal number (range 001 to 366)
%k - Hour (24-hour clock) single digits are preceded by a blank. (range 0 to 23)
%l - hour as a decimal number using a 12-hour clock, single digits preceeded by
a space (range 1 to 12)
%m - month as a decimal number (range 01 to 12)
%M - minute as a decimal number
%n - newline character
%p - either `am' or `pm' according to the given time value, or the corresponding strings for the current locale
%r - time in a.m. and p.m. notation
%R - time in 24 hour notation
%S - second as a decimal number
%t - tab character
%T - current time, equal to %H:%M:%S
%u - weekday as a decimal number [1,7], with 1 representing Monday
%U - week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
%V - The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1
is the first week that has at least 4 days in the current year, and with Monday as the first day of the week.
%w - day of the week as a decimal, Sunday being 0
%W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week
%x - preferred date representation for the current locale without the time
%X - preferred time representation for the current locale without the date
%y - year as a decimal number without a century (range 00 to 99)
%Y - year as a decimal number including the century
%Z - time zone or name or abbreviation
%% - a literal `%' character
PROGRAMMERS NOTE: date_format is essentially a wrapper to PHP's strftime()
function. You may have more or less conversion specifiers available depending
on your system's strftime() function where PHP was compiled. Check your
system's manpage for a full list of valid specifiers. |
|
This is used to set a default value for a variable. If the variable
is empty or unset, the given default value is printed instead.
Default takes one argument.
Example 3-19. default {* this will display "no title" (without the quotes) if $articleTitle is empty *}
{$articleTitle|default:"no title"}
OUTPUT:
no title |
|
This is used to html escape, url escape, escape single quotes on a
variable not already escaped, hex escape or hexentity escape. hex
and hexentity escape can be used in conjunction to hide "mailto:"
links on a page from web spiders (spam collectors) and yet keep
them readable and linkable. By default, the variable is html
escaped.
Example 3-20. escape {$articleTitle}
{$articleTitle|escape}
{$articleTitle|escape:"html"}
{$articleTitle|escape:"url"}
{$articleTitle|escape:"quotes"}
<a
href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
OUTPUT:
'Stiff Opposition Expected to Casketless Funeral Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff+Opposition+Expected+to+Casketless+Funeral+Plan'
\'Stiff Opposition Expected to Casketless Funeral Plan\'
<a
href="mailto:%62%6f%62%40%6d%65%2e%6e%65%74">bob@me.net</a> |
|
This indents a string at each line, default is 4. As
an optional parameter, you can specify the number of characters to
indent. As an optional second parameter, you can specify the
character to use to indent with. (Use "\t" for tabs.)
Example 3-21. indent {$articleTitle}
{$articleTitle|indent}
{$articleTitle|indent:10}
{$articleTitle|indent:1:"\t"}
OUTPUT:
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25. |
|
This is used to lowercase a variable.
Example 3-22. lower {$articleTitle}
{$articleTitle|lower}
OUTPUT:
Two Convicts Evade Noose, Jury Hung.
two convicts evade noose, jury hung. |
|
A regular expression search and replace on a variable. Use the
syntax for preg_replace() from the PHP manual.
NOTE: This function was added to Smarty 1.4.3.
Example 3-23. regex_replace {* replace each carriage return, tab & new line with a space *}
{$articleTitle}
{$articleTitle|regex_replace:"/[\r\t\n]/":" "}
OUTPUT:
Infertility unlikely to
be passed on, experts say
Infertility unlikely to be passed on, experts say |
|
A simple search and replace on a variable.
Example 3-24. replace {$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}
OUTPUT:
Child's Stool Great for Use in Garden.
Child's Stool Great for Use in Vineyard.
Child's Stool Great for Use in Garden. |
|
spacify is a way to insert a space between every character of a variable.
You can optionally pass a different character (or string) to insert.
Example 3-25. spacify {$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}
OUTPUT:
Something Went Wrong in Jet Crash, Experts Say.
S o m e t h i n g W e n t W r o n g i n J e t C r a s h , E x p e r t s S a y .
S^^o^^m^^e^^t^^h^^i^^n^^g^^ ^^W^^e^^n^^t^^ ^^W^^r^^o^^n^^g^^ ^^i^^n^^ ^^J^^e^^t^^ ^^C^^r^^a^^s^^h^^,^^ ^^E^^x^^p^^e^^r^^t^^s^^ ^^S^^a^^y^^. |
|
This is a way to format strings, such as decimal numbers and such.
Use the syntax for sprintf for the formatting.
Example 3-26. string_format {$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}
OUTPUT:
23.5787446
23.58
24 |
|
This strips out markup tags, basically anything between < and >.
Example 3-27. strip_tags {$articleTitle}
{$articleTitle|strip_tags}
OUTPUT:
Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.
Blind Woman Gets New Kidney from Dad she Hasn't Seen in years. |
|
This truncates a variable to a character length, default is 80. As
an optional second parameter, you can specify a string of text
to display at the end if the variable was truncated. The
characters in the string are included with the original truncation length.
By default, truncate will attempt to cut off at a word boundary. If
you want to cut off at the exact character length, pass the optional
third parameter of true.
Example 3-28. truncate {$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
OUTPUT:
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after...
Two Sisters Reunite after
Two Sisters Reunite after---
Two Sisters Reunite after Eigh
Two Sisters Reunite after E... |
|
This is used to uppercase a variable.
Example 3-29. upper {$articleTitle}
{$articleTitle|upper}
OUTPUT:
If Strike isn't Settled Quickly it may Last a While.
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE. |
|
This wraps a string to a column width, default is 80. As
an optional second parameter, you can specify a string of text
to wrap the text to the next line (default is carriage return \n).
By default, wordwrap will attempt to wrap at a word boundary. If
you want to cut off at the exact character length, pass the optional
third parameter of true.
Example 3-30. wordwrap {$articleTitle}
{$articleTitle|wordwrap:30}
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:"<br>\n"}
{$articleTitle|wordwrap:30:"\n":true}
OUTPUT:
Blind woman gets new kidney from dad she hasn't seen in years.
Blind woman gets new kidney
from dad she hasn't seen in
years.
Blind woman gets new
kidney from dad she
hasn't seen in
years.
Blind woman gets new kidney<br>
from dad she hasn't seen in years.
Blind woman gets new kidney fr
om dad she hasn't seen in
years. |
|