aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/smarty/smarty/docs/designers/language-modifiers.md
blob: 4cb69cd1eb4782cf82d5ab596d184861bc9ec908 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
Variable Modifiers {#language.modifiers}
==================

## Table of contents
- [capitalize](./language-modifiers/language-modifier-capitalize.md)
- [cat](./language-modifiers/language-modifier-cat.md)
- [count_characters](./language-modifiers/language-modifier-count-characters.md)
- [count_paragraphs](./language-modifiers/language-modifier-count-paragraphs.md)
- [count_sentences](./language-modifiers/language-modifier-count-sentences.md)
- [count_words](./language-modifiers/language-modifier-count-words.md)
- [date_format](./language-modifiers/language-modifier-date-format.md)
- [default](./language-modifiers/language-modifier-default.md)
- [escape](./language-modifiers/language-modifier-escape.md)
- [from_charset](./language-modifiers/language-modifier-from-charset.md)
- [indent](./language-modifiers/language-modifier-indent.md)
- [lower](./language-modifiers/language-modifier-lower.md)
- [nl2br](./language-modifiers/language-modifier-nl2br.md)
- [regex_replace](./language-modifiers/language-modifier-regex-replace.md)
- [replace](./language-modifiers/language-modifier-replace.md)
- [spacify](./language-modifiers/language-modifier-spacify.md)
- [string_format](./language-modifiers/language-modifier-string-format.md)
- [strip](./language-modifiers/language-modifier-strip.md)
- [strip_tags](./language-modifiers/language-modifier-strip-tags.md)
- [to_charset](./language-modifiers/language-modifier-to-charset.md)
- [truncate](./language-modifiers/language-modifier-truncate.md)
- [unescape](./language-modifiers/language-modifier-unescape.md)
- [upper](./language-modifiers/language-modifier-upper.md)
- [wordwrap](./language-modifiers/language-modifier-wordwrap.md)

Variable modifiers can be applied to
[variables](./language-variables.md), [custom
functions](./language-custom-functions.md) or strings. To apply a modifier,
specify the value followed by a `|` (pipe) and the modifier name. A
modifier may accept additional parameters that affect its behavior.
These parameters follow the modifier name and are separated by a `:`
(colon). Also, *all php-functions can be used as modifiers implicitly*
(more below) and modifiers can be
[combined](./language-combining-modifiers.md).


    {* 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="smarty@example.com"}

    {* using  php's str_repeat *}
    {"="|str_repeat:80}

    {* php's count *}
    {$myArray|@count}

    {* this will uppercase and truncate the whole array *}
    <select name="name_id">
    {html_options output=$my_array|upper|truncate:20}
    </select>

      

- Modifiers can be applied to any type of variables, including arrays
    and objects.

    > **Note**
    >
    > The default behavior was changed with Smarty 3. In Smarty 2.x, you
    > had to use an \"`@`\" symbol to apply a modifier to an array, such
    > as `{$articleTitle|@count}`. With Smarty 3, the \"`@`\" is no
    > longer necessary, and is ignored.
    >
    > If you want a modifier to apply to each individual item of an
    > array, you will either need to loop the array in the template, or
    > provide for this functionality inside your modifier function.

    > **Note**
    >
    > Second, in Smarty 2.x, modifiers were applied to the result of
    > math expressions like `{8+2}`, meaning that
    > `{8+2|count_characters}` would give `2`, as 8+2=10 and 10 is two
    > characters long. With Smarty 3, modifiers are applied to the
    > variables or atomic expressions before executing the calculations,
    > so since 2 is one character long, `{8+2|count_characters}`
    > gives 9. To get the old result use parentheses like
    > `{(8+2)|count_characters}`.

- Modifiers are autoloaded from the
    [`$plugins_dir`](../programmers/api-variables/variable-plugins-dir.md) or can be registered
    explicitly with the [`registerPlugin()`](../programmers/api-functions/api-register-plugin.md)
    function. The later is useful for sharing a function between php
    scripts and smarty templates.

- All php-functions can be used as modifiers implicitly, as
    demonstrated in the example above. However, using php-functions as
    modifiers has two little pitfalls:

    -   First - sometimes the order of the function-parameters is not
        the desirable one. Formatting `$foo` with
        `{"%2.f"|sprintf:$foo}` actually works, but asks for the more
        intuitive, like `{$foo|string_format:"%2.f"}` that is provided
        by the Smarty distribution.

    -   Secondly - if security is enabled, all php-functions that are to
        be used as modifiers have to be declared trusted in the
        `$modifiers` property of the securty policy. See the
        [Security](../programmers/advanced-features/advanced-features-security.md) section for details.

See also [`registerPlugin()`](../programmers/api-functions/api-register-plugin.md), [combining
modifiers](./language-combining-modifiers.md). and [extending smarty with
plugins](../programmers/plugins.md)