1 #============================================================= -*-perl-*-
3 # Template::Manual::Filters
6 # Andy Wardley <abw@wardley.org>
9 # Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
11 # This module is free software; you can redistribute it and/or
12 # modify it under the same terms as Perl itself.
14 #========================================================================
18 Template::Manual::Filters - Standard filters
22 The C<format> filter takes a format string as a parameter (as per
23 C<printf()>) and formats each line of text accordingly.
25 [% FILTER format('<!-- %-40s -->') %]
26 This is a block of text filtered
27 through the above format.
32 <!-- This is a block of text filtered -->
33 <!-- through the above format. -->
37 Folds the input to UPPER CASE.
39 [% "hello world" FILTER upper %]
47 Folds the input to lower case.
49 [% "Hello World" FILTER lower %]
57 Folds the first character of the input to UPPER CASE.
59 [% "hello" FILTER ucfirst %]
67 Folds the first character of the input to lower case.
69 [% "HELLO" FILTER lcfirst %]
77 Trims any leading or trailing whitespace from the input text. Particularly
78 useful in conjunction with C<INCLUDE>, C<PROCESS>, etc., having the same effect
79 as the C<TRIM> configuration option.
81 [% INCLUDE myfile | trim %]
85 Collapse any whitespace sequences in the input text into a single space.
86 Leading and trailing whitespace (which would be reduced to a single space)
87 is removed, as per trim.
101 The cat sat on the mat
105 Converts the characters C<E<lt>>, C<E<gt>>, C<&> and C<"> to C<<>,
106 C<>>, C<&>, and C<"> respectively, protecting them from being
107 interpreted as representing HTML tags or entities.
110 Binary "<=>" returns -1, 0, or 1 depending on...
115 Binary "<=>" returns -1, 0, or 1 depending on...
119 The C<html> filter is fast and simple but it doesn't encode the full
120 range of HTML entities that your text may contain. The C<html_entity>
121 filter uses either the C<Apache::Util> module (which is written in C and
122 is therefore faster) or the C<HTML::Entities> module (written in Perl but
123 equally as comprehensive) to perform the encoding.
125 If one or other of these modules are installed on your system then the text
126 will be encoded (via the C<escape_html()> or C<encode_entities()> subroutines
127 respectively) to convert all extended characters into their appropriate HTML
128 entities (e.g. converting 'C<é>' to 'C<é>'). If neither module is
129 available on your system then an 'C<html_entity>' exception will be thrown
130 reporting an appropriate message.
132 If you want to force TT to use one of the above modules in preference to
133 the other, then call either of the L<Template::Filters> class methods:
134 L<use_html_entities()|Template::Filters/use_html_entities()> or
135 L<use_apache_util()|Template::Filters/use_apache_util()>.
137 use Template::Filters;
138 Template::Filters->use_html_entities;
140 For further information on HTML entity encoding, see
141 L<http://www.w3.org/TR/REC-html40/sgml/entities.html>.
145 Same as the C<html> filter, but adds C<'> which is the fifth XML
150 This filter formats a block of text into HTML paragraphs. A sequence of
151 two or more newlines is used as the delimiter for paragraphs which are
152 then wrapped in HTML C<E<lt>pE<gt>>...C<E<lt>/pE<gt>> tags.
154 [% FILTER html_para %]
155 The cat sat on the mat.
157 Mary had a little lamb.
163 The cat sat on the mat.
167 Mary had a little lamb.
170 =head1 html_break / html_para_break
172 Similar to the html_para filter described above, but uses the HTML tag
173 sequence C<E<lt>brE<gt>E<lt>brE<gt>> to join paragraphs.
175 [% FILTER html_break %]
176 The cat sat on the mat.
178 Mary had a little lamb.
183 The cat sat on the mat.
186 Mary had a little lamb.
188 =head1 html_line_break
190 This filter replaces any newlines with C<E<lt>brE<gt>> HTML tags,
191 thus preserving the line breaks of the original text in the
194 [% FILTER html_line_break %]
195 The cat sat on the mat.
196 Mary had a little lamb.
201 The cat sat on the mat.<br>
202 Mary had a little lamb.<br>
206 This filter URI escapes the input text, converting any characters
207 outside of the permitted URI character set (as defined by RFC 2396)
208 into a C<%nn> hex escape.
210 [% 'my file.html' | uri %]
216 The uri filter correctly encodes all reserved characters, including
217 C<&>, C<@>, C</>, C<;>, C<:>, C<=>, C<+>, C<?> and C<$>. This filter
218 is typically used to encode parameters in a URL that could otherwise
219 be interpreted as part of the URL. Here's an example:
221 [% path = 'http://tt2.org/example'
222 back = '/other?foo=bar&baz=bam'
223 title = 'Earth: "Mostly Harmless"'
225 <a href="[% path %]?back=[% back | uri %]&title=[% title | uri %]">
227 The output generated is rather long so we'll show it split across two
230 <a href="http://tt2.org/example?back=%2Fother%3Ffoo%3Dbar%26
231 baz%3Dbam&title=Earth%3A%20%22Mostly%20Harmless%22">
233 Without the uri filter the output would look like this (also split across
236 <a href="http://tt2.org/example?back=/other?foo=bar
237 &baz=bam&title=Earth: "Mostly Harmless"">
239 In this rather contrived example we've manage to generate both a broken URL
240 (the repeated C<?> is not allowed) and a broken HTML element (the href
241 attribute is terminated by the first C<"> after C<Earth: > leaving C<Mostly
242 Harmless"> dangling on the end of the tag in precisely the way that harmless
243 things shouldn't dangle). So don't do that. Always use the uri filter to
244 encode your URL parameters.
246 However, you should B<not> use the uri filter to encode an entire URL.
248 <a href="[% page_url | uri %]"> # WRONG!
250 This will incorrectly encode any reserved characters like C<:> and C</>
251 and that's almost certainly not what you want in this case. Instead
252 you should use the B<url> (note spelling) filter for this purpose.
254 <a href="[% page_url | url %]"> # CORRECT
256 Please note that this behaviour was changed in version 2.16 of the
257 Template Toolkit. Prior to that, the uri filter did not encode the
258 reserved characters, making it technically incorrect according to the
259 RFC 2396 specification. So we fixed it in 2.16 and provided the url
260 filter to implement the old behaviour of not encoding reserved
265 The url filter is a less aggressive version of the uri filter. It encodes
266 any characters outside of the permitted URI character set (as defined by RFC 2396)
267 into C<%nn> hex escapes. However, unlike the uri filter, the url filter does
268 B<not> encode the reserved characters C<&>, C<@>, C</>, C<;>, C<:>, C<=>, C<+>,
273 Indents the text block by a fixed pad string or width. The 'C<pad>' argument
274 can be specified as a string, or as a numerical value to indicate a pad
275 width (spaces). Defaults to 4 spaces if unspecified.
277 [% FILTER indent('ME> ') %]
279 cabbages, rhubard, onions
285 ME> cabbages, rhubard, onions
287 =head1 truncate(length,dots)
289 Truncates the text block to the length specified, or a default length
290 of 32. Truncated text will be terminated with 'C<...>' (i.e. the 'C<...>'
291 falls inside the required length, rather than appending to it).
293 [% FILTER truncate(21) %]
294 I have much to say on this matter that has previously
295 been said on more than one occasion.
300 I have much to say...
302 If you want to use something other than 'C<...>' you can pass that as a
305 [% FILTER truncate(26, '…') %]
306 I have much to say on this matter that has previously
307 been said on more than one occasion.
312 I have much to say…
314 =head1 repeat(iterations)
316 Repeats the text block for as many iterations as are specified (default: 1).
318 [% FILTER repeat(3) %]
319 We want more beer and we want more beer,
321 We are the more beer wanters!
325 We want more beer and we want more beer,
326 We want more beer and we want more beer,
327 We want more beer and we want more beer,
328 We are the more beer wanters!
330 =head1 remove(string)
332 Searches the input text for any occurrences of the specified string and
333 removes them. A Perl regular expression may be specified as the search
336 [% "The cat sat on the mat" FILTER remove('\s+') %]
342 =head1 replace(search, replace)
344 Similar to the remove filter described above, but taking a second parameter
345 which is used as a replacement string for instances of the search string.
347 [% "The cat sat on the mat" | replace('\s+', '_') %]
351 The_cat_sat_on_the_mat
353 =head1 redirect(file, options)
355 The C<redirect> filter redirects the output of the block into a separate
356 file, specified relative to the C<OUTPUT_PATH> configuration item.
358 [% FOREACH user IN myorg.userlist %]
359 [% FILTER redirect("users/${user.id}.html") %]
360 [% INCLUDE userinfo %]
364 or more succinctly, using side-effect notation:
366 [% FOREACH user IN myorg.userlist;
368 FILTER redirect("users/${user.id}.html");
372 A C<file> exception will be thrown if the C<OUTPUT_PATH> option is undefined.
374 An optional C<binmode> argument can follow the filename to explicitly set
375 the output file to binary mode.
377 [% PROCESS my/png/generator
378 FILTER redirect("images/logo.png", binmode=1) %]
380 For backwards compatibility with earlier versions, a single true/false
381 value can be used to set binary mode.
383 [% PROCESS my/png/generator
384 FILTER redirect("images/logo.png", 1) %]
386 For the sake of future compatibility and clarity, if nothing else, we
387 would strongly recommend you explicitly use the named C<binmode> option
388 as shown in the first example.
392 The C<eval> filter evaluates the block as template text, processing
393 any directives embedded within it. This allows template variables to
394 contain template fragments, or for some method to be provided for
395 returning template fragments from an external source such as a
396 database, which can then be processed in the template as required.
399 fragment => "The cat sat on the [% place %]",
401 $template->process($file, $vars);
403 The following example:
405 [% fragment | eval %]
407 is therefore equivalent to
409 The cat sat on the [% place %]
411 The C<evaltt> filter is provided as an alias for C<eval>.
413 =head1 perl / evalperl
415 The C<perl> filter evaluates the block as Perl code. The C<EVAL_PERL>
416 option must be set to a true value or a C<perl> exception will be
419 [% my_perl_code | perl %]
421 In most cases, the C<[% PERL %]> ... C<[% END %]> block should suffice for
422 evaluating Perl code, given that template directives are processed
423 before being evaluate as Perl. Thus, the previous example could have
424 been written in the more verbose form:
436 The C<evalperl> filter is provided as an alias for C<perl> for backwards
439 =head1 stdout(options)
441 The stdout filter prints the output generated by the enclosing block to
442 C<STDOUT>. The C<binmode> option can be passed as either a named parameter
443 or a single argument to set C<STDOUT> to binary mode (see the
444 binmode perl function).
446 [% PROCESS something/cool
447 FILTER stdout(binmode=1) # recommended %]
449 [% PROCESS something/cool
450 FILTER stdout(1) # alternate %]
452 The C<stdout> filter can be used to force C<binmode> on C<STDOUT>, or also
453 inside C<redirect>, C<null> or C<stderr> blocks to make sure that particular
454 output goes to C<STDOUT>. See the C<null> filter below for an example.
458 The stderr filter prints the output generated by the enclosing block to
463 The C<null> filter prints nothing. This is useful for plugins whose
464 methods return values that you don't want to appear in the output.
465 Rather than assigning every plugin method call to a dummy variable
466 to silence it, you can wrap the block in a null filter:
469 USE im = GD.Image(100,100);
470 black = im.colorAllocate(0, 0, 0);
471 red = im.colorAllocate(255,0, 0);
472 blue = im.colorAllocate(0, 0, 255);
473 im.arc(50,50,95,75,0,360,blue);
479 Notice the use of the C<stdout> filter to ensure that a particular expression
480 generates output to C<STDOUT> (in this case in binary mode).
486 # perl-indent-level: 4
487 # indent-tabs-mode: nil
490 # vim: expandtab shiftwidth=4: