Files
smarty/docs/en/designers/language-modifiers.xml
pete_morgan 3954d5495c tweek
2005-09-10 12:46:35 +00:00

119 lines
4.6 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[
{* 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="me@domain.dom"}
]]>
</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>).</para>
<para>
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. For example:<literal>{$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>
<para>
See also
<link linkend="api.register.modifier">register_modifier()</link>,
<link linkend="api.register.function">register_function()</link>,
<link linkend="plugins">Extending Smarty with plugins</link>
and
<link linkend="plugins.modifiers">modifiers</link>,
</para>
&designers.language-modifiers.language-modifier-capitalize;
&designers.language-modifiers.language-modifier-cat;
&designers.language-modifiers.language-modifier-count-characters;
&designers.language-modifiers.language-modifier-count-paragraphs;
&designers.language-modifiers.language-modifier-count-sentences;
&designers.language-modifiers.language-modifier-count-words;
&designers.language-modifiers.language-modifier-date-format;
&designers.language-modifiers.language-modifier-default;
&designers.language-modifiers.language-modifier-escape;
&designers.language-modifiers.language-modifier-indent;
&designers.language-modifiers.language-modifier-lower;
&designers.language-modifiers.language-modifier-nl2br;
&designers.language-modifiers.language-modifier-regex-replace;
&designers.language-modifiers.language-modifier-replace;
&designers.language-modifiers.language-modifier-spacify;
&designers.language-modifiers.language-modifier-string-format;
&designers.language-modifiers.language-modifier-strip;
&designers.language-modifiers.language-modifier-strip-tags;
&designers.language-modifiers.language-modifier-truncate;
&designers.language-modifiers.language-modifier-upper;
&designers.language-modifiers.language-modifier-wordwrap;
</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
-->