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       <http://www.w3.org/TR/REC-html40/sgml/entities.html>.
121

xml

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

html_para

127       This filter formats a block of text into HTML paragraphs.  A sequence
128       of two or more newlines is used as the delimiter for paragraphs which
129       are then wrapped in HTML "<p>"..."</p>" tags.
130
131           [% FILTER html_para %]
132           The cat sat on the mat.
133
134           Mary had a little lamb.
135           [% END %]
136
137       Output:
138
139           <p>
140           The cat sat on the mat.
141           </p>
142
143           <p>
144           Mary had a little lamb.
145           </p>
146

html_break / html_para_break

148       Similar to the html_para filter described above, but uses the HTML tag
149       sequence "<br><br>" to join paragraphs.
150
151           [% FILTER html_break %]
152           The cat sat on the mat.
153
154           Mary had a little lamb.
155           [% END %]
156
157       Output:
158
159           The cat sat on the mat.
160           <br>
161           <br>
162           Mary had a little lamb.
163

html_line_break

165       This filter replaces any newlines with "<br>" HTML tags, thus
166       preserving the line breaks of the original text in the HTML output.
167
168           [% FILTER html_line_break %]
169           The cat sat on the mat.
170           Mary had a little lamb.
171           [% END %]
172
173       Output:
174
175           The cat sat on the mat.<br>
176           Mary had a little lamb.<br>
177

uri

179       This filter URI escapes the input text, converting any characters
180       outside of the permitted URI character set (as defined by RFC 2396)
181       into a %nn hex escape.
182
183           [% 'my file.html' | uri %]
184
185       Output:
186
187           my%20file.html
188
189       The uri filter correctly encodes all reserved characters, including
190       "&", "@", "/", ";", ":", "=", "+", "?" and "$".  This filter is
191       typically used to encode parameters in a URL that could otherwise be
192       interpreted as part of the URL.  Here's an example:
193
194           [% path  = 'http://tt2.org/example'
195              back  = '/other?foo=bar&baz=bam'
196              title = 'Earth: "Mostly Harmless"'
197           %]
198           <a href="[% path %]?back=[% back | uri %]&title=[% title | uri %]">
199
200       The output generated is rather long so we'll show it split across two
201       lines:
202
203           <a href="http://tt2.org/example?back=%2Fother%3Ffoo%3Dbar%26
204           baz%3Dbam&title=Earth%3A%20%22Mostly%20Harmless%22">
205
206       Without the uri filter the output would look like this (also split
207       across two lines).
208
209           <a href="http://tt2.org/example?back=/other?foo=bar
210           &baz=bam&title=Earth: "Mostly Harmless"">
211
212       In this rather contrived example we've manage to generate both a broken
213       URL (the repeated "?" is not allowed) and a broken HTML element (the
214       href attribute is terminated by the first """ after "Earth: " leaving
215       "Mostly Harmless"" dangling on the end of the tag in precisely the way
216       that harmless things shouldn't dangle). So don't do that. Always use
217       the uri filter to encode your URL parameters.
218
219       However, you should not use the uri filter to encode an entire URL.
220
221          <a href="[% page_url | uri %]">   # WRONG!
222
223       This will incorrectly encode any reserved characters like ":" and "/"
224       and that's almost certainly not what you want in this case.  Instead
225       you should use the url (note spelling) filter for this purpose.
226
227          <a href="[% page_url | url %]">   # CORRECT
228
229       Please note that this behaviour was changed in version 2.16 of the
230       Template Toolkit.  Prior to that, the uri filter did not encode the
231       reserved characters, making it technically incorrect according to the
232       RFC 2396 specification.  So we fixed it in 2.16 and provided the url
233       filter to implement the old behaviour of not encoding reserved
234       characters.
235

url

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

indent(pad)

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

truncate(length,dots)

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

repeat(iterations)

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

remove(string)

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

replace(search, replace)

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

redirect(file, options)

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

eval / evaltt

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

perl / evalperl

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

stdout(options)

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

stderr

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

null

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