Smarty: Variable Modifiers-Lower

lower

This is used to lowercase a variable.

Example 5-12. lower
<?php
    
$smarty
= new Smarty;
$smarty->assign('articleTitle', 'Two Convicts Evade Noose, Jury Hung.');
$smarty->display('index.tpl');
    
?>

Where index.tpl is:

{$articleTitle}
{$articleTitle|lower}

This will output:

Two Convicts Evade Noose, Jury Hung.
two convicts evade noose, jury hung.

nl2br

All linebreaks will be converted to <br /> tags in the given variable. This is equivalent to the PHP nl2br() function.

Example 5-13. nl2br
<?php

$smarty
= new Smarty;
$smarty->assign('articleTitle', "Sun or rain expected\ntoday, dark tonight");
$smarty->display('index.tpl');

?>

Where index.tpl is:

{$articleTitle|nl2br}

This should output:

Sun or rain expected<br />today, dark tonight

regex_replace

Parameter Position Type Required Default Description
1 string Yes n/a This is the regular expression to be replaced.
2 string Yes n/a This is the string of text to replace with.

A regular expression search and replace on a variable. Use the syntax for preg_replace() from the PHP manual.

Example 5-14. regex_replace
<?php

$smarty
= new Smarty;
$smarty->assign('articleTitle', "Infertility unlikely to\nbe passed on, experts say.");
$smarty->display('index.tpl');

?>

Where index.tpl is:

{* replace each carriage return, tab and new line with a space *}

{$articleTitle}
{$articleTitle|regex_replace:"/[\r\t\n]/":" "}

This should output:

Infertility unlikely to
be passed on, experts say.
Infertility unlikely to be passed on, experts say.

replace

Parameter Position Type Required Default Description
1 string Yes n/a This is the string of text to be replaced.
2 string Yes n/a This is the string of text to replace with.

A simple search and replace on a variable.

Example 5-15. replace
<?php

$smarty
= new Smarty;
$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");
$smarty->display('index.tpl');

?>

Where index.tpl is:

{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}

This should 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

Parameter Position Type Required Default Description
1 string No one space This what gets inserted between each character of the variable.

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 5-16. spacify
<?php
$smarty
= new Smarty;
$smarty->assign('articleTitle', 'Something Went Wrong in Jet Crash, Experts Say.');
$smarty->display('index.tpl');
?>

Where index.tpl is:

{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}

This should 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^^.

string_format

Parameter Position Type Required Default Description
1 string Yes n/a This is what format to use. (sprintf)

This is a way to format strings, such as decimal numbers and such. Use the syntax for sprintf for the formatting.

Example 5-17. string_format
<?php

$smarty
= new Smarty;
$smarty->assign('number', 23.5787446);
$smarty->display('index.tpl');

?>

Where index.tpl is:

{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}

This should output:

23.5787446
23.58
24

strip

This replaces all repeated spaces, newlines and tabs with a single space, or with a supplied string.

Note: If you want to strip blocks of template text, use the strip function.

Example 5-18. strip
<?php

$smarty
= new Smarty;
$smarty->assign('articleTitle', "Grandmother of\neight makes\t    hole in one.");
$smarty->display('index.tpl');

?>

where index.tpl is:

{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:"&nbsp;"}

This will output:

Grandmother of
eight makes hole in one.
Grandmother of eight makes hole in one.
Grandmother&nbsp;of&nbsp;eight&nbsp;makes&nbsp;hole&nbsp;in&nbsp;one.

strip_tags

Parameter Position Type Required Default Description
1 bool No true This determines wether the tags are replaced by ' ' or by ''

This strips out markup tags, basically anything between < and >.

Example 5-19. strip_tags
<?php

$smarty
= new Smarty;
$smarty->assign('articleTitle', "Blind Woman Gets <font face=\"helvetica\">New
Kidney</font> from Dad she Hasn't Seen in <b>years</b>."
);
$smarty->display('index.tpl');

?>

where index.tpl is:

{$articleTitle}
{$articleTitle|strip_tags} {* same as {$articleTitle|strip_tags:true} *}
{$articleTitle|strip_tags:false}

This will 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 .
Blind Woman Gets New Kidney from Dad she Hasn't Seen in years.

truncate

Parameter Position Type Required Default Description
1 integer No 80 This determines how many characters to truncate to.
2 string No ... This is the text to append if truncation occurs.
3 boolean No false This determines whether or not to truncate at a word boundary (false), or at the exact character (true).

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 5-20. truncate
<?php
    
$smarty
= new Smarty;
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
$smarty->display('index.tpl');

?>

where index.tpl is:

{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}

This will 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...

upper

This is used to uppercase a variable.

Example 5-21. upper
<?php

$smarty
= new Smarty;
$smarty->assign('articleTitle', "If Strike isn't Settled Quickly it may Last a While.");
$smarty->display('index.tpl');

?>

where index.tpl is:

{$articleTitle}
{$articleTitle|upper}

This will output:

If Strike isn't Settled Quickly it may Last a While.
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.

wordwrap

Parameter Position Type Required Default Description
1 integer No 80 This determines how many columns to wrap to.
2 string No \n This is the string used to wrap words with.
3 boolean No false This determines whether or not to wrap at a word boundary (false), or at the exact character (true).

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 5-22. wordwrap
<?php
    
$smarty
= new Smarty;
$smarty->assign('articleTitle', "Blind woman gets new kidney from dad she hasn't seen in years.");
$smarty->display('index.tpl');

?>

where index.tpl is:

{$articleTitle}

{$articleTitle|wordwrap:30}

{$articleTitle|wordwrap:20}

{$articleTitle|wordwrap:30:"<br />\n"}

{$articleTitle|wordwrap:30:"\n":true}

This will 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<br />
years.

Blind woman gets new kidney
from dad she hasn't seen in
years.