1Template::Manual::FilteUrsse(r3)Contributed Perl DocumenTteamtpiloante::Manual::Filters(3)
2
3
4
6 Template::Manual::Filters - Standard filters
7
9 The "format" filter takes a format string as a parameter (as per
10 "printf()") and formats each line of text accordingly.
11
12 [% FILTER format('<!-- %-40s -->') %]
13 This is a block of text filtered
14 through the above format.
15 [% END %]
16
17 Output:
18
19 <!-- This is a block of text filtered -->
20 <!-- through the above format. -->
21
23 Folds the input to UPPER CASE.
24
25 [% "hello world" FILTER upper %]
26
27 Output:
28
29 HELLO WORLD
30
32 Folds the input to lower case.
33
34 [% "Hello World" FILTER lower %]
35
36 Output:
37
38 hello world
39
41 Folds the first character of the input to UPPER CASE.
42
43 [% "hello" FILTER ucfirst %]
44
45 Output:
46
47 Hello
48
50 Folds the first character of the input to lower case.
51
52 [% "HELLO" FILTER lcfirst %]
53
54 Output:
55
56 hELLO
57
59 Trims any leading or trailing whitespace from the input text.
60 Particularly useful in conjunction with "INCLUDE", "PROCESS", etc.,
61 having the same effect as the "TRIM" configuration option.
62
63 [% INCLUDE myfile | trim %]
64
66 Collapse any whitespace sequences in the input text into a single
67 space. Leading and trailing whitespace (which would be reduced to a
68 single space) is removed, as per trim.
69
70 [% FILTER collapse %]
71
72 The cat
73
74 sat on
75
76 the mat
77
78 [% END %]
79
80 Output:
81
82 The cat sat on the mat
83
85 Converts the characters "<", ">", "&" and """ to "<", ">",
86 "&", and """ respectively, protecting them from being
87 interpreted as representing HTML tags or entities.
88
89 [% FILTER html %]
90 Binary "<=>" returns -1, 0, or 1 depending on...
91 [% END %]
92
93 Output:
94
95 Binary "<=>" returns -1, 0, or 1 depending on...
96
98 The "html" filter is fast and simple but it doesn't encode the full
99 range of HTML entities that your text may contain. The "html_entity"
100 filter uses either the "Apache::Util" module (which is written in C and
101 is therefore faster) or the "HTML::Entities" module (written in Perl
102 but equally as comprehensive) to perform the encoding.
103
104 If one or other of these modules are installed on your system then the
105 text will be encoded (via the "escape_html()" or "encode_entities()"
106 subroutines respectively) to convert all extended characters into their
107 appropriate HTML entities (e.g. converting '"?"' to '"é"'). If
108 neither module is available on your system then an '"html_entity"'
109 exception will be thrown reporting an appropriate message.
110
111 If you want to force TT to use one of the above modules in preference
112 to the other, then call either of the Template::Filters class methods:
113 use_html_entities() or use_apache_util().
114
115 use Template::Filters;
116 Template::Filters->use_html_entities;
117
118 For further information on HTML entity encoding, see
119 <http://www.w3.org/TR/REC-html40/sgml/entities.html>.
120
122 Same as the "html" filter, but adds "'" which is the fifth XML
123 built-in entity.
124
126 This filter formats a block of text into HTML paragraphs. A sequence
127 of two or more newlines is used as the delimiter for paragraphs which
128 are then wrapped in HTML "<p>"..."</p>" tags.
129
130 [% FILTER html_para %]
131 The cat sat on the mat.
132
133 Mary had a little lamb.
134 [% END %]
135
136 Output:
137
138 <p>
139 The cat sat on the mat.
140 </p>
141
142 <p>
143 Mary had a little lamb.
144 </p>
145
147 Similar to the html_para filter described above, but uses the HTML tag
148 sequence "<br><br>" to join paragraphs.
149
150 [% FILTER html_break %]
151 The cat sat on the mat.
152
153 Mary had a little lamb.
154 [% END %]
155
156 Output:
157
158 The cat sat on the mat.
159 <br>
160 <br>
161 Mary had a little lamb.
162
164 This filter replaces any newlines with "<br>" HTML tags, thus
165 preserving the line breaks of the original text in the HTML output.
166
167 [% FILTER html_line_break %]
168 The cat sat on the mat.
169 Mary had a little lamb.
170 [% END %]
171
172 Output:
173
174 The cat sat on the mat.<br>
175 Mary had a little lamb.<br>
176
178 This filter URI escapes the input text, converting any characters
179 outside of the permitted URI character set (as defined by RFC 3986)
180 into a %nn hex escape.
181
182 [% 'my file.html' | uri %]
183
184 Output:
185
186 my%20file.html
187
188 The uri filter correctly encodes all reserved characters, including
189 "&", "@", "/", ";", ":", "=", "+", "?" and "$". This filter is
190 typically used to encode parameters in a URL that could otherwise be
191 interpreted as part of the URL. Here's an example:
192
193 [% path = 'http://tt2.org/example'
194 back = '/other?foo=bar&baz=bam'
195 title = 'Earth: "Mostly Harmless"'
196 %]
197 <a href="[% path %]?back=[% back | uri %]&title=[% title | uri %]">
198
199 The output generated is rather long so we'll show it split across two
200 lines:
201
202 <a href="http://tt2.org/example?back=%2Fother%3Ffoo%3Dbar%26
203 baz%3Dbam&title=Earth%3A%20%22Mostly%20Harmless%22">
204
205 Without the uri filter the output would look like this (also split
206 across two lines).
207
208 <a href="http://tt2.org/example?back=/other?foo=bar
209 &baz=bam&title=Earth: "Mostly Harmless"">
210
211 In this rather contrived example we've manage to generate both a broken
212 URL (the repeated "?" is not allowed) and a broken HTML element (the
213 href attribute is terminated by the first """ after "Earth: " leaving
214 "Mostly Harmless"" dangling on the end of the tag in precisely the way
215 that harmless things shouldn't dangle). So don't do that. Always use
216 the uri filter to encode your URL parameters.
217
218 However, you should not use the uri filter to encode an entire URL.
219
220 <a href="[% page_url | uri %]"> # WRONG!
221
222 This will incorrectly encode any reserved characters like ":" and "/"
223 and that's almost certainly not what you want in this case. Instead
224 you should use the url (note spelling) filter for this purpose.
225
226 <a href="[% page_url | url %]"> # CORRECT
227
228 Please note that this behaviour was changed in version 2.16 of the
229 Template Toolkit. Prior to that, the uri filter did not encode the
230 reserved characters, making it technically incorrect according to the
231 RFC 2396 specification (since superceded by RFC2732 and RFC3986). So
232 we fixed it in 2.16 and provided the url filter to implement the old
233 behaviour of not encoding reserved characters.
234
235 As of version 2.26 of the Template Toolkit, the "uri" and url filters
236 use the unsafe character set defined by RFC3986. This means that
237 certain characters ("(", ")", "~", "*", "!" and the single quote "'")
238 are now deemed unsafe and will be escaped as hex character sequences.
239 The double quote character ('"') is now deemed safe and will not be
240 escaped.
241
242 If you want to enable the old behaviour then call the "use_rfc2732()"
243 method in Template::Filters
244
245 use Template::Filters
246 Template::Filters->use_rfc2732;
247
249 The url filter is a less aggressive version of the uri filter. It
250 encodes any characters outside of the permitted URI character set (as
251 defined by RFC 2396) into %nn hex escapes. However, unlike the uri
252 filter, the url filter does not encode the reserved characters "&",
253 "@", "/", ";", ":", "=", "+", "?" and "$".
254
256 Indents the text block by a fixed pad string or width. The '"pad"'
257 argument can be specified as a string, or as a numerical value to
258 indicate a pad width (spaces). Defaults to 4 spaces if unspecified.
259
260 [% FILTER indent('ME> ') %]
261 blah blah blah
262 cabbages, rhubard, onions
263 [% END %]
264
265 Output:
266
267 ME> blah blah blah
268 ME> cabbages, rhubard, onions
269
271 Truncates the text block to the length specified, or a default length
272 of 32. Truncated text will be terminated with '"..."' (i.e. the
273 '"..."' falls inside the required length, rather than appending to
274 it).
275
276 [% FILTER truncate(21) %]
277 I have much to say on this matter that has previously
278 been said on more than one occasion.
279 [% END %]
280
281 Output:
282
283 I have much to say...
284
285 If you want to use something other than '"..."' you can pass that as a
286 second argument.
287
288 [% FILTER truncate(26, '…') %]
289 I have much to say on this matter that has previously
290 been said on more than one occasion.
291 [% END %]
292
293 Output:
294
295 I have much to say…
296
298 Repeats the text block for as many iterations as are specified
299 (default: 1).
300
301 [% FILTER repeat(3) %]
302 We want more beer and we want more beer,
303 [% END %]
304 We are the more beer wanters!
305
306 Output:
307
308 We want more beer and we want more beer,
309 We want more beer and we want more beer,
310 We want more beer and we want more beer,
311 We are the more beer wanters!
312
314 Searches the input text for any occurrences of the specified string and
315 removes them. A Perl regular expression may be specified as the search
316 string.
317
318 [% "The cat sat on the mat" FILTER remove('\s+') %]
319
320 Output:
321
322 Thecatsatonthemat
323
325 Similar to the remove filter described above, but taking a second
326 parameter which is used as a replacement string for instances of the
327 search string.
328
329 [% "The cat sat on the mat" | replace('\s+', '_') %]
330
331 Output:
332
333 The_cat_sat_on_the_mat
334
336 The "redirect" filter redirects the output of the block into a separate
337 file, specified relative to the "OUTPUT_PATH" configuration item.
338
339 [% FOREACH user IN myorg.userlist %]
340 [% FILTER redirect("users/${user.id}.html") %]
341 [% INCLUDE userinfo %]
342 [% END %]
343 [% END %]
344
345 or more succinctly, using side-effect notation:
346
347 [% FOREACH user IN myorg.userlist;
348 INCLUDE userinfo
349 FILTER redirect("users/${user.id}.html");
350 END
351 %]
352
353 A "file" exception will be thrown if the "OUTPUT_PATH" option is
354 undefined.
355
356 An optional "binmode" argument can follow the filename to explicitly
357 set the output file to binary mode.
358
359 [% PROCESS my/png/generator
360 FILTER redirect("images/logo.png", binmode=1) %]
361
362 For backwards compatibility with earlier versions, a single true/false
363 value can be used to set binary mode.
364
365 [% PROCESS my/png/generator
366 FILTER redirect("images/logo.png", 1) %]
367
368 For the sake of future compatibility and clarity, if nothing else, we
369 would strongly recommend you explicitly use the named "binmode" option
370 as shown in the first example.
371
373 The "eval" filter evaluates the block as template text, processing any
374 directives embedded within it. This allows template variables to
375 contain template fragments, or for some method to be provided for
376 returning template fragments from an external source such as a
377 database, which can then be processed in the template as required.
378
379 my $vars = {
380 fragment => "The cat sat on the [% place %]",
381 };
382 $template->process($file, $vars);
383
384 The following example:
385
386 [% fragment | eval %]
387
388 is therefore equivalent to
389
390 The cat sat on the [% place %]
391
392 The "evaltt" filter is provided as an alias for "eval".
393
395 The "perl" filter evaluates the block as Perl code. The "EVAL_PERL"
396 option must be set to a true value or a "perl" exception will be
397 thrown.
398
399 [% my_perl_code | perl %]
400
401 In most cases, the "[% PERL %]" ... "[% END %]" block should suffice
402 for evaluating Perl code, given that template directives are processed
403 before being evaluate as Perl. Thus, the previous example could have
404 been written in the more verbose form:
405
406 [% PERL %]
407 [% my_perl_code %]
408 [% END %]
409
410 as well as
411
412 [% FILTER perl %]
413 [% my_perl_code %]
414 [% END %]
415
416 The "evalperl" filter is provided as an alias for "perl" for backwards
417 compatibility.
418
420 The stdout filter prints the output generated by the enclosing block to
421 "STDOUT". The "binmode" option can be passed as either a named
422 parameter or a single argument to set "STDOUT" to binary mode (see the
423 binmode perl function).
424
425 [% PROCESS something/cool
426 FILTER stdout(binmode=1) # recommended %]
427
428 [% PROCESS something/cool
429 FILTER stdout(1) # alternate %]
430
431 The "stdout" filter can be used to force "binmode" on "STDOUT", or also
432 inside "redirect", "null" or "stderr" blocks to make sure that
433 particular output goes to "STDOUT". See the "null" filter below for an
434 example.
435
437 The stderr filter prints the output generated by the enclosing block to
438 "STDERR".
439
441 The "null" filter prints nothing. This is useful for plugins whose
442 methods return values that you don't want to appear in the output.
443 Rather than assigning every plugin method call to a dummy variable to
444 silence it, you can wrap the block in a null filter:
445
446 [% FILTER null;
447 USE im = GD.Image(100,100);
448 black = im.colorAllocate(0, 0, 0);
449 red = im.colorAllocate(255,0, 0);
450 blue = im.colorAllocate(0, 0, 255);
451 im.arc(50,50,95,75,0,360,blue);
452 im.fill(50,50,red);
453 im.png | stdout(1);
454 END;
455 -%]
456
457 Notice the use of the "stdout" filter to ensure that a particular
458 expression generates output to "STDOUT" (in this case in binary mode).
459
460
461
462perl v5.28.0 2014-04-23 Template::Manual::Filters(3)