| 148 |
lars |
1 |
Modifiers {#plugins.modifiers}
|
|
|
2 |
=========
|
|
|
3 |
|
|
|
4 |
[Modifiers](#language.modifiers) are little functions that are applied
|
|
|
5 |
to a variable in the template before it is displayed or used in some
|
|
|
6 |
other context. Modifiers can be chained together.
|
|
|
7 |
|
|
|
8 |
mixed
|
|
|
9 |
|
|
|
10 |
smarty\_modifier\_
|
|
|
11 |
|
|
|
12 |
name
|
|
|
13 |
|
|
|
14 |
mixed
|
|
|
15 |
|
|
|
16 |
\$value
|
|
|
17 |
|
|
|
18 |
\[mixed
|
|
|
19 |
|
|
|
20 |
\$param1
|
|
|
21 |
|
|
|
22 |
, \...\]
|
|
|
23 |
|
|
|
24 |
The first parameter to the modifier plugin is the value on which the
|
|
|
25 |
modifier is to operate. The rest of the parameters are optional,
|
|
|
26 |
depending on what kind of operation is to be performed.
|
|
|
27 |
|
|
|
28 |
The modifier has to [return](&url.php-manual;return) the result of its
|
|
|
29 |
processing.
|
|
|
30 |
|
|
|
31 |
This plugin basically aliases one of the built-in PHP functions. It does
|
|
|
32 |
not have any additional parameters.
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
<?php
|
|
|
36 |
/*
|
|
|
37 |
* Smarty plugin
|
|
|
38 |
* -------------------------------------------------------------
|
|
|
39 |
* File: modifier.capitalize.php
|
|
|
40 |
* Type: modifier
|
|
|
41 |
* Name: capitalize
|
|
|
42 |
* Purpose: capitalize words in the string
|
|
|
43 |
* -------------------------------------------------------------
|
|
|
44 |
*/
|
|
|
45 |
function smarty_modifier_capitalize($string)
|
|
|
46 |
{
|
|
|
47 |
return ucwords($string);
|
|
|
48 |
}
|
|
|
49 |
?>
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
<?php
|
|
|
53 |
/*
|
|
|
54 |
* Smarty plugin
|
|
|
55 |
* -------------------------------------------------------------
|
|
|
56 |
* File: modifier.truncate.php
|
|
|
57 |
* Type: modifier
|
|
|
58 |
* Name: truncate
|
|
|
59 |
* Purpose: Truncate a string to a certain length if necessary,
|
|
|
60 |
* optionally splitting in the middle of a word, and
|
|
|
61 |
* appending the $etc string.
|
|
|
62 |
* -------------------------------------------------------------
|
|
|
63 |
*/
|
|
|
64 |
function smarty_modifier_truncate($string, $length = 80, $etc = '...',
|
|
|
65 |
$break_words = false)
|
|
|
66 |
{
|
|
|
67 |
if ($length == 0)
|
|
|
68 |
return '';
|
|
|
69 |
|
|
|
70 |
if (strlen($string) > $length) {
|
|
|
71 |
$length -= strlen($etc);
|
|
|
72 |
$fragment = substr($string, 0, $length+1);
|
|
|
73 |
if ($break_words)
|
|
|
74 |
$fragment = substr($fragment, 0, -1);
|
|
|
75 |
else
|
|
|
76 |
$fragment = preg_replace('/\s+(\S+)?$/', '', $fragment);
|
|
|
77 |
return $fragment.$etc;
|
|
|
78 |
} else
|
|
|
79 |
return $string;
|
|
|
80 |
}
|
|
|
81 |
?>
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
See also [`registerPlugin()`](#api.register.plugin),
|
|
|
86 |
[`unregisterPlugin()`](#api.unregister.plugin).
|