1Template::Manual::FilteUrsse(r3)Contributed Perl DocumenTteamtpiloante::Manual::Filters(3)
2
3
4

NAME

6       Template::Manual::Filters - Standard filters
7

format(format)

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

upper

23       Folds the input to UPPER CASE.
24
25           [% "hello world" FILTER upper %]
26
27       Output:
28
29           HELLO WORLD
30

lower

32       Folds the input to lower case.
33
34           [% "Hello World" FILTER lower %]
35
36       Output:
37
38           hello world
39

ucfirst

41       Folds the first character of the input to UPPER CASE.
42
43           [% "hello" FILTER ucfirst %]
44
45       Output:
46
47           Hello
48

lcfirst

50       Folds the first character of the input to lower case.
51
52           [% "HELLO" FILTER lcfirst %]
53
54       Output:
55
56           hELLO
57

trim

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

collapse

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

html

85       Converts the characters "<", ">", "&" and """ to "&lt;", "&gt;",
86       "&amp;", and "&quot;" 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 "&lt;=&gt;" returns -1, 0, or 1 depending on...
96

html_entity

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 '"e"' to '"&eacute;"'). 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

xml

122       Same as the "html" filter, but adds "&apos;" which is the fifth XML
123       built-in entity.
124

html_para

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

html_break / html_para_break

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

html_line_break

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

uri

178       This filter URI escapes the input text, converting any characters
179       outside of the permitted URI character set (as defined by RFC 2396)
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.  So we fixed it in 2.16 and provided the url
232       filter to implement the old behaviour of not encoding reserved
233       characters.
234

url

236       The url filter is a less aggressive version of the uri filter.  It
237       encodes any characters outside of the permitted URI character set (as
238       defined by RFC 2396) into %nn hex escapes.  However, unlike the uri
239       filter, the url filter does not encode the reserved characters "&",
240       "@", "/", ";", ":", "=", "+", "?" and "$".
241

indent(pad)

243       Indents the text block by a fixed pad string or width.  The '"pad"'
244       argument can be specified as a string, or as a numerical value to
245       indicate a pad width (spaces).  Defaults to 4 spaces if unspecified.
246
247           [% FILTER indent('ME> ') %]
248           blah blah blah
249           cabbages, rhubard, onions
250           [% END %]
251
252       Output:
253
254           ME> blah blah blah
255           ME> cabbages, rhubard, onions
256

truncate(length,dots)

258       Truncates the text block to the length specified, or a default length
259       of 32.  Truncated text will be terminated with '"..."' (i.e. the
260       '"..."'  falls inside the required length, rather than appending to
261       it).
262
263           [% FILTER truncate(21) %]
264           I have much to say on this matter that has previously
265           been said on more than one occasion.
266           [% END %]
267
268       Output:
269
270           I have much to say...
271
272       If you want to use something other than '"..."' you can pass that as a
273       second argument.
274
275           [% FILTER truncate(26, '&hellip;') %]
276           I have much to say on this matter that has previously
277           been said on more than one occasion.
278           [% END %]
279
280       Output:
281
282           I have much to say&hellip;
283

repeat(iterations)

285       Repeats the text block for as many iterations as are specified
286       (default: 1).
287
288           [% FILTER repeat(3) %]
289           We want more beer and we want more beer,
290           [% END %]
291           We are the more beer wanters!
292
293       Output:
294
295           We want more beer and we want more beer,
296           We want more beer and we want more beer,
297           We want more beer and we want more beer,
298           We are the more beer wanters!
299

remove(string)

301       Searches the input text for any occurrences of the specified string and
302       removes them.  A Perl regular expression may be specified as the search
303       string.
304
305           [% "The  cat  sat  on  the  mat" FILTER remove('\s+') %]
306
307       Output:
308
309           Thecatsatonthemat
310

replace(search, replace)

312       Similar to the remove filter described above, but taking a second
313       parameter which is used as a replacement string for instances of the
314       search string.
315
316           [% "The  cat  sat  on  the  mat" | replace('\s+', '_') %]
317
318       Output:
319
320           The_cat_sat_on_the_mat
321

redirect(file, options)

323       The "redirect" filter redirects the output of the block into a separate
324       file, specified relative to the "OUTPUT_PATH" configuration item.
325
326           [% FOREACH user IN myorg.userlist %]
327              [% FILTER redirect("users/${user.id}.html") %]
328                 [% INCLUDE userinfo %]
329              [% END %]
330           [% END %]
331
332       or more succinctly, using side-effect notation:
333
334           [%  FOREACH user IN myorg.userlist;
335                 INCLUDE userinfo
336                   FILTER redirect("users/${user.id}.html");
337               END
338           %]
339
340       A "file" exception will be thrown if the "OUTPUT_PATH" option is
341       undefined.
342
343       An optional "binmode" argument can follow the filename to explicitly
344       set the output file to binary mode.
345
346           [% PROCESS my/png/generator
347                FILTER redirect("images/logo.png", binmode=1) %]
348
349       For backwards compatibility with earlier versions, a single true/false
350       value can be used to set binary mode.
351
352           [% PROCESS my/png/generator
353                FILTER redirect("images/logo.png", 1) %]
354
355       For the sake of future compatibility and clarity, if nothing else, we
356       would strongly recommend you explicitly use the named "binmode" option
357       as shown in the first example.
358

eval / evaltt

360       The "eval" filter evaluates the block as template text, processing any
361       directives embedded within it.  This allows template variables to
362       contain template fragments, or for some method to be provided for
363       returning template fragments from an external source such as a
364       database, which can then be processed in the template as required.
365
366           my $vars  = {
367               fragment => "The cat sat on the [% place %]",
368           };
369           $template->process($file, $vars);
370
371       The following example:
372
373           [% fragment | eval %]
374
375       is therefore equivalent to
376
377           The cat sat on the [% place %]
378
379       The "evaltt" filter is provided as an alias for "eval".
380

perl / evalperl

382       The "perl" filter evaluates the block as Perl code.  The "EVAL_PERL"
383       option must be set to a true value or a "perl" exception will be
384       thrown.
385
386           [% my_perl_code | perl %]
387
388       In most cases, the "[% PERL %]" ... "[% END %]" block should suffice
389       for evaluating Perl code, given that template directives are processed
390       before being evaluate as Perl.  Thus, the previous example could have
391       been written in the more verbose form:
392
393           [% PERL %]
394           [% my_perl_code %]
395           [% END %]
396
397       as well as
398
399           [% FILTER perl %]
400           [% my_perl_code %]
401           [% END %]
402
403       The "evalperl" filter is provided as an alias for "perl" for backwards
404       compatibility.
405

stdout(options)

407       The stdout filter prints the output generated by the enclosing block to
408       "STDOUT".  The "binmode" option can be passed as either a named
409       parameter or a single argument to set "STDOUT" to binary mode (see the
410       binmode perl function).
411
412           [% PROCESS something/cool
413                  FILTER stdout(binmode=1) # recommended %]
414
415           [% PROCESS something/cool
416                  FILTER stdout(1)         # alternate %]
417
418       The "stdout" filter can be used to force "binmode" on "STDOUT", or also
419       inside "redirect", "null" or "stderr" blocks to make sure that
420       particular output goes to "STDOUT". See the "null" filter below for an
421       example.
422

stderr

424       The stderr filter prints the output generated by the enclosing block to
425       "STDERR".
426

null

428       The "null" filter prints nothing.  This is useful for plugins whose
429       methods return values that you don't want to appear in the output.
430       Rather than assigning every plugin method call to a dummy variable to
431       silence it, you can wrap the block in a null filter:
432
433           [% FILTER null;
434               USE im = GD.Image(100,100);
435               black = im.colorAllocate(0,   0, 0);
436               red   = im.colorAllocate(255,0,  0);
437               blue  = im.colorAllocate(0,  0,  255);
438               im.arc(50,50,95,75,0,360,blue);
439               im.fill(50,50,red);
440               im.png | stdout(1);
441              END;
442           -%]
443
444       Notice the use of the "stdout" filter to ensure that a particular
445       expression generates output to "STDOUT" (in this case in binary mode).
446

POD ERRORS

448       Hey! The above document had some coding errors, which are explained
449       below:
450
451       Around line 128:
452           Non-ASCII character seen before =encoding in ''C<e>''. Assuming
453           ISO8859-1
454
455
456
457perl v5.16.3                      2011-12-20      Template::Manual::Filters(3)
Impressum