aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/smarty/smarty/docs/programmers/plugins/plugins-modifiers.md
blob: b089821a61c89fb6bfff594fd36616a6bc1ddf19 (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
Modifiers {#plugins.modifiers}
=========

[Modifiers](#language.modifiers) are little functions that are applied
to a variable in the template before it is displayed or used in some
other context. Modifiers can be chained together.

mixed

smarty\_modifier\_

name

mixed

\$value

\[mixed

\$param1

, \...\]

The first parameter to the modifier plugin is the value on which the
modifier is to operate. The rest of the parameters are optional,
depending on what kind of operation is to be performed.

The modifier has to [return](&url.php-manual;return) the result of its
processing.

This plugin basically aliases one of the built-in PHP functions. It does
not have any additional parameters.


    <?php
    /*
     * Smarty plugin
     * -------------------------------------------------------------
     * File:     modifier.capitalize.php
     * Type:     modifier
     * Name:     capitalize
     * Purpose:  capitalize words in the string
     * -------------------------------------------------------------
     */
    function smarty_modifier_capitalize($string)
    {
        return ucwords($string);
    }
    ?>


    <?php
    /*
     * Smarty plugin
     * -------------------------------------------------------------
     * File:     modifier.truncate.php
     * Type:     modifier
     * Name:     truncate
     * Purpose:  Truncate a string to a certain length if necessary,
     *           optionally splitting in the middle of a word, and
     *           appending the $etc string.
     * -------------------------------------------------------------
     */
    function smarty_modifier_truncate($string, $length = 80, $etc = '...',
                                      $break_words = false)
    {
        if ($length == 0)
            return '';

        if (strlen($string) > $length) {
            $length -= strlen($etc);
            $fragment = substr($string, 0, $length+1);
            if ($break_words)
                $fragment = substr($fragment, 0, -1);
            else
                $fragment = preg_replace('/\s+(\S+)?$/', '', $fragment);
            return $fragment.$etc;
        } else
            return $string;
    }
    ?>

         

See also [`registerPlugin()`](#api.register.plugin),
[`unregisterPlugin()`](#api.unregister.plugin).