Skip to content

Commit fd75d4d

Browse files
committed
Improve documentation of template modifiers
* Moved them to a separate page * Improved general explanation * Explained how to replace deprecated modifiers * Documented whitelisted PHP functions that can be used as a variable modifier
1 parent 1c7520e commit fd75d4d

File tree

4 files changed

+315
-192
lines changed

4 files changed

+315
-192
lines changed

docs/view/template-modifiers.md

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
# Template Modifiers
2+
3+
Variable modifiers are used to modify the output of variables within templates.
4+
These modifiers allow you to perform various operations on the variables before displaying them.
5+
The general syntax for applying a variable modifier is `{$variable|modifier}`.
6+
7+
Modifiers can be chained together to perform multiple operations on a variable. In such cases, the modifiers are applied from left to right. For example:
8+
```smarty
9+
{$variable|modifier1|modifier2|modifier3}
10+
```
11+
12+
A modifier may accept additional parameters that affect its behavior. These parameters follow the modifier name and are separated by a `:`. For example:
13+
```smarty
14+
{$variable|modifier:'param1':'param2'}
15+
```
16+
17+
## Build-in Modifiers
18+
19+
### `|concat`
20+
21+
`concat` is a modifier used to concatenate multiple strings:
22+
23+
```smarty
24+
{assign var=foo value='foo'}
25+
26+
{assign var=templateVariable value='bar'|concat:$foo}
27+
28+
{$templateVariable} {* prints 'foobar *}
29+
```
30+
31+
### `|currency`
32+
33+
`currency` is a modifier used to format currency values with two decimals using language dependent thousands separators and decimal point:
34+
35+
```smarty
36+
{assign var=currencyValue value=12.345}
37+
38+
{$currencyValue|currency} {* prints '12.34' *}
39+
```
40+
41+
### `|date`
42+
43+
!!! info "This template plugin has been deprecated in WoltLab Suite 6.0. Use `{time type='plainDate'}` or `{time type='custom'}` instead."
44+
45+
`date` generated a formatted date using `wcf\util\DateUtil::format()` with `DateUtil::DATE_FORMAT` internally.
46+
47+
```smarty
48+
{$timestamp|date}
49+
```
50+
51+
### `|encodeJS`
52+
53+
`encodeJS` encodes a string to be used as a single-quoted string in JavaScript by replacing `\\` with `\\\\`, `'` with `\'`, linebreaks with `\n`, and `/` with `\/`.
54+
55+
```smarty
56+
<script>
57+
var foo = '{@$foo|encodeJS}';
58+
</script>
59+
```
60+
61+
### `|escapeCDATA`
62+
63+
`escapeCDATA` encodes a string to be used in a `CDATA` element by replacing `]]>` with `]]]]><![CDATA[>`.
64+
65+
```smarty
66+
<![CDATA[{@$foo|encodeCDATA}]]>
67+
```
68+
69+
70+
### `|filesizeBinary`
71+
72+
`filesizeBinary` formats the filesize using binary filesize (in bytes).
73+
74+
```smarty
75+
{$filesize|filesizeBinary}
76+
```
77+
78+
79+
### `|filesize`
80+
81+
`filesize` formats the filesize using filesize (in bytes).
82+
83+
```smarty
84+
{$filesize|filesize}
85+
```
86+
87+
### `|ipSearch`
88+
89+
`ipSearch` generates a link to search for an IP address.
90+
91+
```smarty
92+
{"127.0.0.1"|ipSearch}
93+
```
94+
95+
### `|json`
96+
97+
`json` JSON-encodes the given value.
98+
99+
```smarty
100+
<script>
101+
let data = { "title": {@$foo->getTitle()|json} };
102+
</script>
103+
```
104+
105+
### `|language`
106+
107+
`language` replaces a language items with its value.
108+
If the template variable `__language` exists, this language object will be used instead of `WCF::getLanguage()`.
109+
This modifier is useful when assigning the value directly to a variable.
110+
111+
Note that template scripting is applied to the output of the variable, which can lead to unwanted side effects. Use `phrase` instead if you don't want to use template scripting.
112+
113+
```smarty
114+
{$languageItem|language}
115+
116+
{assign var=foo value=$languageItem|language}
117+
```
118+
119+
### `|newlineToBreak`
120+
121+
`newlineToBreak` transforms newlines into HTML `<br>` elements after encoding the content via `wcf\util\StringUtil::encodeHTML()`.
122+
123+
```smarty
124+
{$foo|newlineToBreak}
125+
```
126+
127+
### `|phrase`
128+
129+
`phrase` replaces a language items with its value.
130+
If the template variable `__language` exists, this language object will be used instead of `WCF::getLanguage()`.
131+
This modifier is useful when assigning the value directly to a variable.
132+
133+
`phrase` should be used instead of `language` unless you want to explicitly allow template scripting on a variable's output.
134+
135+
```smarty
136+
{$languageItem|phrase}
137+
138+
{assign var=foo value=$languageItem|phrase}
139+
```
140+
141+
### `|plainTime`
142+
143+
!!! info "This template plugin has been deprecated in WoltLab Suite 6.0. Use `{time type='plainTime'}` instead."
144+
145+
`plainTime` formats a timestamp to include year, month, day, hour, and minutes.
146+
The exact formatting depends on the current language (via the language items `wcf.date.dateTimeFormat`, `wcf.date.dateFormat`, and `wcf.date.timeFormat`).
147+
148+
```smarty
149+
{$timestamp|plainTime}
150+
```
151+
152+
### `|shortUnit`
153+
154+
`shortUnit` shortens numbers larger than 1000 by using unit suffixes:
155+
156+
```smarty
157+
{10000|shortUnit} {* prints 10k *}
158+
{5400000|shortUnit} {* prints 5.4M *}
159+
```
160+
161+
162+
### `|tableWordwrap`
163+
164+
`tableWordwrap` inserts zero width spaces every 30 characters in words longer than 30 characters.
165+
166+
```smarty
167+
{$foo|tableWordwrap}
168+
```
169+
170+
### `|time`
171+
172+
!!! info "This template plugin has been deprecated in WoltLab Suite 6.0. Use `{time}` instead."
173+
174+
`time` generates an HTML `time` elements based on a timestamp that shows a relative time or the absolute time if the timestamp more than six days ago.
175+
176+
```smarty
177+
{$timestamp|time} {* prints a '<time>' element *}
178+
```
179+
180+
181+
### `|truncate`
182+
183+
`truncate` truncates a long string into a shorter one:
184+
185+
```smarty
186+
{$foo|truncate:35}
187+
188+
{$foo|truncate:35:'_':true}
189+
```
190+
191+
192+
| Parameter Number | Description |
193+
|-----------|-------------|
194+
| 0 | truncated string |
195+
| 1 | truncated length; `80` by default |
196+
| 2 | ellipsis symbol; `wcf\util\StringUtil::HELLIP` by default |
197+
| 3 | if `true`, words can be broken up in the middle; `false` by default |
198+
199+
200+
## PHP Functions
201+
202+
A whitelisted set of PHP functions can also be used as variable modifiers:
203+
204+
* `abs`
205+
* `addslashes`
206+
* `array_diff`
207+
* `array_fill`
208+
* `array_key_exists`
209+
* `array_keys`
210+
* `array_pop`
211+
* `array_slice`
212+
* `array_values`
213+
* `base64_decode`
214+
* `base64_encode`
215+
* `basename`
216+
* `ceil`
217+
* `concat`
218+
* `constant`
219+
* `count`
220+
* `currency`
221+
* `current`
222+
* `date`
223+
* `defined`
224+
* `doubleval`
225+
* `empty`
226+
* `end`
227+
* `explode`
228+
* `file_exists`
229+
* `filesize`
230+
* `floatval`
231+
* `floor`
232+
* `function_exists`
233+
* `get_class`
234+
* `gmdate`
235+
* `hash`
236+
* `htmlspecialchars`
237+
* `html_entity_decode`
238+
* `http_build_query`
239+
* `implode`
240+
* `in_array`
241+
* `is_array`
242+
* `is_null`
243+
* `is_numeric`
244+
* `is_object`
245+
* `is_string`
246+
* `iterator_count`
247+
* `intval`
248+
* `is_subclass_of`
249+
* `isset`
250+
* `json_encode`
251+
* `key`
252+
* `lcfirst`
253+
* `ltrim`
254+
* `max`
255+
* `mb_strpos`
256+
* `mb_strlen`
257+
* `mb_strpos`
258+
* `mb_strtolower`
259+
* `mb_strtoupper`
260+
* `mb_substr`
261+
* `md5`
262+
* `method_exists`
263+
* `microtime`
264+
* `min`
265+
* `nl2br`
266+
* `number_format`
267+
* `parse_url`
268+
* `preg_match`
269+
* `preg_replace`
270+
* `print_r`
271+
* `random_int`
272+
* `rawurlencode`
273+
* `reset`
274+
* `round`
275+
* `sha1`
276+
* `spl_object_hash`
277+
* `sprintf`
278+
* `strip_tags`
279+
* `strlen`
280+
* `strpos`
281+
* `strtolower`
282+
* `strtotime`
283+
* `strtoupper`
284+
* `str_contains`
285+
* `str_ends_with`
286+
* `str_ireplace`
287+
* `str_pad`
288+
* `str_repeat`
289+
* `str_replace`
290+
* `str_starts_with`
291+
* `substr`
292+
* `trim`
293+
* `ucfirst`
294+
* `uniqid`
295+
* `urlencode`
296+
* `var_dump`
297+
* `version_compare`
298+
* `wcfDebug`
299+
* `wordwrap`

0 commit comments

Comments
 (0)