| 991 |
lars |
1 |
# Variable Modifiers
|
|
|
2 |
|
|
|
3 |
Variable modifiers can be applied to
|
|
|
4 |
[variables](../language-variables/index.md), [custom functions](../language-custom-functions/index.md)
|
|
|
5 |
or strings. To apply a modifier,
|
|
|
6 |
specify the value followed by a `|` (pipe) and the modifier name. A
|
|
|
7 |
modifier may accept additional parameters that affect its behavior.
|
|
|
8 |
These parameters follow the modifier name and are separated by a `:`
|
|
|
9 |
(colon). Also, *all php-functions can be used as modifiers implicitly*
|
|
|
10 |
(more below) and modifiers can be
|
|
|
11 |
[combined](../language-combining-modifiers.md).
|
|
|
12 |
|
|
|
13 |
- [capitalize](language-modifier-capitalize.md)
|
|
|
14 |
- [cat](language-modifier-cat.md)
|
|
|
15 |
- [count_characters](language-modifier-count-characters.md)
|
|
|
16 |
- [count_paragraphs](language-modifier-count-paragraphs.md)
|
|
|
17 |
- [count_sentences](language-modifier-count-sentences.md)
|
|
|
18 |
- [count_words](language-modifier-count-words.md)
|
|
|
19 |
- [date_format](language-modifier-date-format.md)
|
|
|
20 |
- [default](language-modifier-default.md)
|
|
|
21 |
- [escape](language-modifier-escape.md)
|
|
|
22 |
- [from_charset](language-modifier-from-charset.md)
|
|
|
23 |
- [indent](language-modifier-indent.md)
|
|
|
24 |
- [lower](language-modifier-lower.md)
|
|
|
25 |
- [nl2br](language-modifier-nl2br.md)
|
|
|
26 |
- [regex_replace](language-modifier-regex-replace.md)
|
|
|
27 |
- [replace](language-modifier-replace.md)
|
|
|
28 |
- [spacify](language-modifier-spacify.md)
|
|
|
29 |
- [string_format](language-modifier-string-format.md)
|
|
|
30 |
- [strip](language-modifier-strip.md)
|
|
|
31 |
- [strip_tags](language-modifier-strip-tags.md)
|
|
|
32 |
- [to_charset](language-modifier-to-charset.md)
|
|
|
33 |
- [truncate](language-modifier-truncate.md)
|
|
|
34 |
- [unescape](language-modifier-unescape.md)
|
|
|
35 |
- [upper](language-modifier-upper.md)
|
|
|
36 |
- [wordwrap](language-modifier-wordwrap.md)
|
|
|
37 |
|
|
|
38 |
## Examples
|
|
|
39 |
|
|
|
40 |
```smarty
|
|
|
41 |
{* apply modifier to a variable *}
|
|
|
42 |
{$title|upper}
|
|
|
43 |
|
|
|
44 |
{* modifier with parameters *}
|
|
|
45 |
{$title|truncate:40:"..."}
|
|
|
46 |
|
|
|
47 |
{* apply modifier to a function parameter *}
|
|
|
48 |
{html_table loop=$myvar|upper}
|
|
|
49 |
|
|
|
50 |
{* with parameters *}
|
|
|
51 |
{html_table loop=$myvar|truncate:40:"..."}
|
|
|
52 |
|
|
|
53 |
{* apply modifier to literal string *}
|
|
|
54 |
{"foobar"|upper}
|
|
|
55 |
|
|
|
56 |
{* using date_format to format the current date *}
|
|
|
57 |
{$smarty.now|date_format:"%Y/%m/%d"}
|
|
|
58 |
|
|
|
59 |
{* apply modifier to a custom function *}
|
|
|
60 |
{mailto|upper address="smarty@example.com"}
|
|
|
61 |
|
|
|
62 |
{* using php's str_repeat *}
|
|
|
63 |
{"="|str_repeat:80}
|
|
|
64 |
|
|
|
65 |
{* php's count *}
|
|
|
66 |
{$myArray|@count}
|
|
|
67 |
|
|
|
68 |
{* this will uppercase and truncate the whole array *}
|
|
|
69 |
<select name="name_id">
|
|
|
70 |
{html_options output=$my_array|upper|truncate:20}
|
|
|
71 |
</select>
|
|
|
72 |
```
|
|
|
73 |
|
|
|
74 |
- Modifiers can be applied to any type of variables, including arrays
|
|
|
75 |
and objects.
|
|
|
76 |
|
|
|
77 |
> **Note**
|
|
|
78 |
>
|
|
|
79 |
> The default behavior was changed with Smarty 3. In Smarty 2.x, you
|
|
|
80 |
> had to use an "`@`" symbol to apply a modifier to an array, such
|
|
|
81 |
> as `{$articleTitle|@count}`. With Smarty 3, the "`@`" is no
|
|
|
82 |
> longer necessary, and is ignored.
|
|
|
83 |
>
|
|
|
84 |
> If you want a modifier to apply to each individual item of an
|
|
|
85 |
> array, you will either need to loop the array in the template, or
|
|
|
86 |
> provide for this functionality inside your modifier function.
|
|
|
87 |
|
|
|
88 |
> **Note**
|
|
|
89 |
>
|
|
|
90 |
> Second, in Smarty 2.x, modifiers were applied to the result of
|
|
|
91 |
> math expressions like `{8+2}`, meaning that
|
|
|
92 |
> `{8+2|count_characters}` would give `2`, as 8+2=10 and 10 is two
|
|
|
93 |
> characters long. With Smarty 3, modifiers are applied to the
|
|
|
94 |
> variables or atomic expressions before executing the calculations,
|
|
|
95 |
> so since 2 is one character long, `{8+2|count_characters}`
|
|
|
96 |
> gives 9. To get the old result use parentheses like
|
|
|
97 |
> `{(8+2)|count_characters}`.
|
|
|
98 |
|
|
|
99 |
- Modifiers are autoloaded from the
|
|
|
100 |
[`$plugins_dir`](../../programmers/api-variables/variable-plugins-dir.md) or can be registered
|
|
|
101 |
explicitly with the [`registerPlugin()`](../../programmers/api-functions/api-register-plugin.md)
|
|
|
102 |
function. The later is useful for sharing a function between php
|
|
|
103 |
scripts and smarty templates.
|
|
|
104 |
|
|
|
105 |
- All php-functions can be used as modifiers implicitly, as
|
|
|
106 |
demonstrated in the example above. However, using php-functions as
|
|
|
107 |
modifiers has two little pitfalls:
|
|
|
108 |
|
|
|
109 |
- First - sometimes the order of the function-parameters is not
|
|
|
110 |
the desirable one. Formatting `$foo` with
|
|
|
111 |
`{"%2.f"|sprintf:$foo}` actually works, but asks for the more
|
|
|
112 |
intuitive, like `{$foo|string_format:"%2.f"}` that is provided
|
|
|
113 |
by the Smarty distribution.
|
|
|
114 |
|
|
|
115 |
- Secondly - if security is enabled, all php-functions that are to
|
|
|
116 |
be used as modifiers have to be declared trusted in the
|
|
|
117 |
`$modifiers` property of the security policy. See the
|
|
|
118 |
[Security](../../programmers/advanced-features/advanced-features-security.md) section for details.
|
|
|
119 |
|
|
|
120 |
See also [`registerPlugin()`](../../programmers/api-functions/api-register-plugin.md), [combining
|
|
|
121 |
modifiers](../language-combining-modifiers.md). and [extending smarty with
|
|
|
122 |
plugins](../../programmers/plugins.md)
|