Files
smarty/docs/en/designers/language-modifiers.xml
2004-04-01 17:06:27 +00:00

1512 lines
38 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<chapter id="language.modifiers">
<title>Variable Modifiers</title>
<para>
Variable modifiers can be applied to variables, custom functions or strings. To
apply a modifier, specify the value followed by the <literal>|</literal>
(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 <literal>:</literal> (colon).
</para>
<example>
<title>modifier example</title>
<programlisting>
<![CDATA[
{* Uppercase the title *}
<h2>{$title|upper}</h2>
{* Truncate the topic to 40 characters use ... at the end *}
Topic: {$topic|truncate:40:"..."}
{* format a literal string *}
{"now"|date_format:"%Y/%m/%d"}
{* apply modifier to a custom function *}
{mailto|upper address="me@domain.dom"}
r
]]>
</programlisting>
</example>
<para>
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 <literal>@</literal> symbol like so:
<literal>{$articleTitle|@count}</literal> (this will print out the number of
elements in the $articleTitle array.)
</para>
<para>
Modifiers can be autoloaded from your <link
linkend="variable.plugins.dir">$plugins_dir</link> (also see:
<link linkend="plugins.naming.conventions">Naming
Conventions</link>) or can be registered explicitely (see: <link
linkend="api.register.modifier">register_modifier</link>). Additionally
all php-functions can be used as modifiers implicitly. (The
<literal>@count</literal>-example above actually uses php's
count-function and not a smarty-modifier). Using php-functions
as modifiers has two little pitfalls: First: Sometimes the order
of the function-parameters is not the desirable one
(<literal>{"%2.f"|sprintf:$float}</literal> actually works, but
asks for the more intuitive <literal>{For example:
$float|string_format:"%2.f"}</literal> that is provided by the
Smarty distribution). Second: with <link
linkend="variable.security">$security</link> turned on all
php-functions that are to be used as modifiers have to be
declared trusted in the <link linkend="variable.security.settings">
$security_settings['MODIFIER_FUNCS']</link>-array.
</para>
<sect1 id="language.modifier.capitalize">
<title>capitalize</title>
<para>
This is used to capitalize the first letter of all words in a variable.
</para>
<example>
<title>capitalize</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|capitalize}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.count.characters">
<title>count_characters</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc" />
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>boolean</entry>
<entry>No</entry>
<entry>false</entry>
<entry>This determines whether or not to include
whitespace characters in the count.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
This is used to count the number of characters in a variable.
</para>
<example>
<title>count_characters</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Cold Wave Linked to Temperatures.');
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|count_characters}
{$articleTitle|count_characters:true}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
Cold Wave Linked to Temperatures.
29
33
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.cat">
<title>cat</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="cat" align="center" />
<colspec colname="desc" />
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>cat</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>No</entry>
<entry><emphasis>empty</emphasis></entry>
<entry>This value to catenate to the given variable.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
This value is concatenated to the given variable.
</para>
<example>
<title>cat</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', "Psychics predict world didn't end");
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle|cat:" yesterday."}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
Psychics predict world didn't end yesterday.
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.count.paragraphs">
<title>count_paragraphs</title>
<para>
This is used to count the number of paragraphs in a variable.
</para>
<example>
<title>count_paragraphs</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', "War Dims Hope for Peace. Child's Death Ruins
Couple's Holiday.\n\nMan is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.");
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|count_paragraphs}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.
Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.
2
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.count.sentences">
<title>count_sentences</title>
<para>
This is used to count the number of sentences in a variable.
</para>
<example>
<title>count_sentences</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.');
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|count_sentences}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.
2
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.count.words">
<title>count_words</title>
<para>
This is used to count the number of words in a variable.
</para>
<example>
<title>count_words</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|count_words}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
Dealers Will Hear Car Talk at Noon.
7
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.date.format">
<title>date_format</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc" />
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>No</entry>
<entry>%b %e, %Y</entry>
<entry>This is the format for the outputted date.</entry>
</row>
<row>
<entry>2</entry>
<entry>string</entry>
<entry>No</entry>
<entry>n/a</entry>
<entry>This is the default date if the input is empty.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
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. If the date passed to date_format is empty
and a second parameter is passed, that will be used as the date to
format.
</para>
<example>
<title>date_format</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$smarty.now|date_format}
{$smarty.now|date_format:"%A, %B %e, %Y"}
{$smarty.now|date_format:"%H:%M:%S"}
{$yesterday|date_format}
{$yesterday|date_format:"%A, %B %e, %Y"}
{$yesterday|date_format:"%H:%M:%S"}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
Feb 6, 2001
Tuesday, February 6, 2001
14:33:00
Feb 5, 2001
Monday, February 5, 2001
14:33:00
]]>
</screen>
</example>
<para>
date_format conversion specifiers:
<itemizedlist>
<listitem><para>
%a - abbreviated weekday name according to the current locale
</para></listitem>
<listitem><para>
%A - full weekday name according to the current locale
</para></listitem>
<listitem><para>
%b - abbreviated month name according to the current locale
</para></listitem>
<listitem><para>
%B - full month name according to the current locale
</para></listitem>
<listitem><para>
%c - preferred date and time representation for the current locale
</para></listitem>
<listitem><para>
%C - century number (the year divided by 100 and truncated to an integer, range 00 to 99)
</para></listitem>
<listitem><para>
%d - day of the month as a decimal number (range 00 to 31)
</para></listitem>
<listitem><para>
%D - same as %m/%d/%y
</para></listitem>
<listitem><para>
%e - day of the month as a decimal number, a single digit is preceded by a space (range 1 to 31)
</para></listitem>
<listitem><para>
%g - Week-based year within century [00,99]
</para></listitem>
<listitem><para>
%G - Week-based year, including the century [0000,9999]
</para></listitem>
<listitem><para>
%h - same as %b
</para></listitem>
<listitem><para>
%H - hour as a decimal number using a 24-hour clock (range 00 to 23)
</para></listitem>
<listitem><para>
%I - hour as a decimal number using a 12-hour clock (range 01 to 12)
</para></listitem>
<listitem><para>
%j - day of the year as a decimal number (range 001 to 366)
</para></listitem>
<listitem><para>
%k - Hour (24-hour clock) single digits are preceded by a blank. (range 0 to 23)
</para></listitem>
<listitem><para>
%l - hour as a decimal number using a 12-hour clock, single digits preceeded by a space (range 1 to 12)
</para></listitem>
<listitem><para>
%m - month as a decimal number (range 01 to 12)
</para></listitem>
<listitem><para>
%M - minute as a decimal number
</para></listitem>
<listitem><para>
%n - newline character
</para></listitem>
<listitem><para>
%p - either `am' or `pm' according to the given time value, or the corresponding strings for the current locale
</para></listitem>
<listitem><para>
%r - time in a.m. and p.m. notation
</para></listitem>
<listitem><para>
%R - time in 24 hour notation
</para></listitem>
<listitem><para>
%S - second as a decimal number
</para></listitem>
<listitem><para>
%t - tab character
</para></listitem>
<listitem><para>
%T - current time, equal to %H:%M:%S
</para></listitem>
<listitem><para>
%u - weekday as a decimal number [1,7], with 1 representing Monday
</para></listitem>
<listitem><para>
%U - week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
</para></listitem>
<listitem><para>
%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.
</para></listitem>
<listitem><para>
%w - day of the week as a decimal, Sunday being 0
</para></listitem>
<listitem><para>
%W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week
</para></listitem>
<listitem><para>
%x - preferred date representation for the current locale without the time
</para></listitem>
<listitem><para>
%X - preferred time representation for the current locale without the date
</para></listitem>
<listitem><para>
%y - year as a decimal number without a century (range 00 to 99)
</para></listitem>
<listitem><para>
%Y - year as a decimal number including the century
</para></listitem>
<listitem><para>
%Z - time zone or name or abbreviation
</para></listitem>
<listitem><para>
%% - a literal `%' character
</para></listitem>
</itemizedlist>
<note>
<title>Programmers note</title>
<para>
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.
</para>
</note>
</para>
</sect1>
<sect1 id="language.modifier.default">
<title>default</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc" />
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>No</entry>
<entry><emphasis>empty</emphasis></entry>
<entry>This is the default value to output if the
variable is empty.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
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.
</para>
<example>
<title>default</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle|default:"no title"}
{$myTitle|default:"no title"}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
Dealers Will Hear Car Talk at Noon.
no title
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.escape">
<title>escape</title>
<informaltable frame="all">
<tgroup cols="6">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="possible" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc" />
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Possible Values</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>No</entry>
<entry>html,htmlall,url,quotes,hex,hexentity,javascript</entry>
<entry>html</entry>
<entry>This is the escape format to use.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
This is used to html escape, url escape, escape single quotes on a
variable not already escaped, hex escape, hexentity or javascript escape.
By default, the variable is html
escaped.
</para>
<example>
<title>escape</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', "'Stiff Opposition Expected to Casketless Funeral Plan'");
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|escape}
{$articleTitle|escape:"html"} {* escapes & " ' < > *}
{$articleTitle|escape:"htmlall"} {* escapes ALL html entities *}
{$articleTitle|escape:"url"}
{$articleTitle|escape:"quotes"}
<a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
'Stiff Opposition Expected to Casketless Funeral Plan'
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
%27Stiff+Opposition+Expected+to+Casketless+Funeral+Plan%27
\'Stiff Opposition Expected to Casketless Funeral Plan\'
<a href="mailto:%62%6f%62%40%6d%65%2e%6e%65%74">&#x62;&#x6f;&#x62;&#x40;&#x6d;&#x65;&#x2e;&#x6e;&#x65;&#x74;</a>
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.indent">
<title>indent</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc" />
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>integer</entry>
<entry>No</entry>
<entry>4</entry>
<entry>This determines how many characters to indent
to.</entry>
</row>
<row>
<entry>2</entry>
<entry>string</entry>
<entry>No</entry>
<entry>(one space)</entry>
<entry>This is the character used to indent with.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
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.)
</para>
<example>
<title>indent</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', 'NJ judge to rule on nude beach.');
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|indent}
{$articleTitle|indent:10}
{$articleTitle|indent:1:"\t"}
]]>
</programlisting>
<para>
this will output:
</para>
<screen>
<![CDATA[
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.
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.lower">
<title>lower</title>
<para>
This is used to lowercase a variable.
</para>
<example>
<title>lower</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Convicts Evade Noose, Jury Hung.');
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|lower}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
Two Convicts Evade Noose, Jury Hung.
two convicts evade noose, jury hung.
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.nl2br">
<title>nl2br</title>
<para>
All linebreaks will be converted to &lt;br /&gt; tags in the given
variable. This is equivalent to the PHP nl2br() function.
</para>
<example>
<title>nl2br</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', "Sun or rain expected\ntoday, dark tonight");
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle|nl2br}
]]>
</programlisting>
<para>
This should output:
</para>
<screen>
<![CDATA[
Sun or rain expected<br />today, dark tonight
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.regex.replace">
<title>regex_replace</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc"/>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>This is the regular expression to be replaced.</entry>
</row>
<row>
<entry>2</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>This is the string of text to replace with.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
A regular expression search and replace on a variable. Use the
syntax for preg_replace() from the PHP manual.
</para>
<example>
<title>regex_replace</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', "Infertility unlikely to\nbe passed on, experts say.");
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{* replace each carriage return, tab and new line with a space *}
{$articleTitle}
{$articleTitle|regex_replace:"/[\r\t\n]/":" "}
]]>
</programlisting>
<para>
This should output:
</para>
<screen>
<![CDATA[
Infertility unlikely to
be passed on, experts say.
Infertility unlikely to be passed on, experts say.
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.replace">
<title>replace</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc"/>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>This is the string of text to be replaced.</entry>
</row>
<row>
<entry>2</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>This is the string of text to replace with.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
A simple search and replace on a variable.
</para>
<example>
<title>replace</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}
]]>
</programlisting>
<para>
This should output:
</para>
<screen>
<![CDATA[
Child's Stool Great for Use in Garden.
Child's Stool Great for Use in Vineyard.
Child's Stool Great for Use in Garden.
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.spacify">
<title>spacify</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc"/>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>No</entry>
<entry><emphasis>one space</emphasis></entry>
<entry>This what gets inserted between each character of
the variable.</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
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.
</para>
<example>
<title>spacify</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Something Went Wrong in Jet Crash, Experts Say.');
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}
]]>
</programlisting>
<para>
This should output:
</para>
<screen>
<![CDATA[
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^^.
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.string.format">
<title>string_format</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc"/>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>string</entry>
<entry>Yes</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>This is what format to use. (sprintf)</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
This is a way to format strings, such as decimal numbers and such.
Use the syntax for sprintf for the formatting.
</para>
<example>
<title>string_format</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('number', 23.5787446);
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
Where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}
]]>
</programlisting>
<para>
This should output:
</para>
<screen>
<![CDATA[
23.5787446
23.58
24
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.strip">
<title>strip</title>
<para>
This replaces all repeated spaces, newlines and tabs with a single
space, or with a supplied string.
</para>
<note>
<title>Note</title>
<para>
If you want to strip blocks of template text, use the <link
linkend="language.function.strip">strip function</link>.
</para>
</note>
<example>
<title>strip</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', "Grandmother of\neight makes\t hole in one.");
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:"&nbsp;"}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
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.
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.strip.tags">
<title>strip_tags</title>
<para>
This strips out markup tags, basically anything between &lt; and &gt;.
</para>
<example>
<title>strip_tags</title>
<programlisting role="php">
<![CDATA[
<?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');
?>
]]>
</programlisting>
<para>
where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|strip_tags}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
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.
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.truncate">
<title>truncate</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc"/>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>integer</entry>
<entry>No</entry>
<entry>80</entry>
<entry>This determines how many characters to truncate
to.</entry>
</row>
<row>
<entry>2</entry>
<entry>string</entry>
<entry>No</entry>
<entry>...</entry>
<entry>This is the text to append if truncation occurs.</entry>
</row>
<row>
<entry>3</entry>
<entry>boolean</entry>
<entry>No</entry>
<entry>false</entry>
<entry>This determines whether or not to truncate at a
word boundary (false), or at the exact character (true).</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
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.
</para>
<example>
<title>truncate</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
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...
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.upper">
<title>upper</title>
<para>
This is used to uppercase a variable.
</para>
<example>
<title>upper</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', "If Strike isn't Settled Quickly it may Last a While.");
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|upper}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
If Strike isn't Settled Quickly it may Last a While.
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.
]]>
</screen>
</example>
</sect1>
<sect1 id="language.modifier.wordwrap">
<title>wordwrap</title>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc"/>
<thead>
<row>
<entry>Parameter Position</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>1</entry>
<entry>integer</entry>
<entry>No</entry>
<entry>80</entry>
<entry>This determines how many columns to wrap
to.</entry>
</row>
<row>
<entry>2</entry>
<entry>string</entry>
<entry>No</entry>
<entry>\n</entry>
<entry>This is the string used to wrap words with.</entry>
</row>
<row>
<entry>3</entry>
<entry>boolean</entry>
<entry>No</entry>
<entry>false</entry>
<entry>This determines whether or not to wrap at a
word boundary (false), or at the exact character (true).</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<para>
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.
</para>
<example>
<title>wordwrap</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->assign('articleTitle', "Blind woman gets new kidney from dad she hasn't seen in years.");
$smarty->display('index.tpl');
?>
]]>
</programlisting>
<para>
where index.tpl is:
</para>
<programlisting>
<![CDATA[
{$articleTitle}
{$articleTitle|wordwrap:30}
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:"<br />\n"}
{$articleTitle|wordwrap:30:"\n":true}
]]>
</programlisting>
<para>
This will output:
</para>
<screen>
<![CDATA[
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.
]]>
</screen>
</example>
</sect1>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->