[% FILTER format('<!-- %-40s -->') %] This is a block of text filtered through the above format. [% END %]
Output:
<!-- This is a block of text filtered --> <!-- through the above format. -->
[% "hello world" FILTER upper %]
Output:
HELLO WORLD
[% "Hello World" FILTER lower %]
Output:
hello world
[% "hello" FILTER ucfirst %]
Output:
Hello
[% "HELLO" FILTER lcfirst %]
Output:
hELLO
[% INCLUDE myfile | trim %]
[% FILTER collapse %] The cat sat on the mat [% END %]
Output:
The cat sat on the mat
[% FILTER html %] Binary "<=>" returns -1, 0, or 1 depending on... [% END %]
Output:
Binary "<=>" returns -1, 0, or 1 depending on...
If one or other of these modules are installed on your system then the text will be encoded (via the "escape_html()" or "encode_entities()" subroutines respectively) to convert all extended characters into their appropriate HTML entities (e.g. converting '"?"' to '"é"'). If neither module is available on your system then an '"html_entity"' exception will be thrown reporting an appropriate message.
If you want to force TT to use one of the above modules in preference to the other, then call either of the Template::Filters class methods: use_html_entities() or use_apache_util().
use Template::Filters; Template::Filters->use_html_entities;
For further information on HTML entity encoding, see <http://www.w3.org/TR/REC-html40/sgml/entities.html>.
[% FILTER html_para %] The cat sat on the mat. Mary had a little lamb. [% END %]
Output:
<p> The cat sat on the mat. </p> <p> Mary had a little lamb. </p>
[% FILTER html_break %] The cat sat on the mat. Mary had a little lamb. [% END %]
Output:
The cat sat on the mat. <br> <br> Mary had a little lamb.
[% FILTER html_line_break %] The cat sat on the mat. Mary had a little lamb. [% END %]
Output:
The cat sat on the mat.<br> Mary had a little lamb.<br>
[% 'my file.html' | uri %]
Output:
my%20file.html
The uri filter correctly encodes all reserved characters, including "&", "@", "/", ";", ":", "=", "+", "?" and "$". This filter is typically used to encode parameters in a URL that could otherwise be interpreted as part of the URL. Here's an example:
[% path = 'http://tt2.org/example' back = '/other?foo=bar&baz=bam' title = 'Earth: "Mostly Harmless"' %] <a href="[% path %]?back=[% back | uri %]&title=[% title | uri %]">
The output generated is rather long so we'll show it split across two lines:
<a href="http://tt2.org/example?back=%2Fother%3Ffoo%3Dbar%26 baz%3Dbam&title=Earth%3A%20%22Mostly%20Harmless%22">
Without the uri filter the output would look like this (also split across two lines).
<a href="http://tt2.org/example?back=/other?foo=bar &baz=bam&title=Earth: "Mostly Harmless"">
In this rather contrived example we've manage to generate both a broken URL (the repeated "?" is not allowed) and a broken HTML element (the href attribute is terminated by the first """ after "Earth: " leaving "Mostly Harmless"" dangling on the end of the tag in precisely the way that harmless things shouldn't dangle). So don't do that. Always use the uri filter to encode your URL parameters.
However, you should not use the uri filter to encode an entire URL.
<a href="[% page_url | uri %]"> # WRONG!
This will incorrectly encode any reserved characters like ":" and "/" and that's almost certainly not what you want in this case. Instead you should use the url (note spelling) filter for this purpose.
<a href="[% page_url | url %]"> # CORRECT
Please note that this behaviour was changed in version 2.16 of the Template Toolkit. Prior to that, the uri filter did not encode the reserved characters, making it technically incorrect according to the RFC 2396 specification (since superceded by RFC2732 and RFC3986). So we fixed it in 2.16 and provided the url filter to implement the old behaviour of not encoding reserved characters.
As of version 2.26 of the Template Toolkit, the "uri" and url filters use the unsafe character set defined by RFC3986. This means that certain characters (``('', ``)'', ``~'', ``*'', ``!'' and the single quote ``''') are now deemed unsafe and will be escaped as hex character sequences. The double quote character ('"') is now deemed safe and will not be escaped.
If you want to enable the old behaviour then call the "use_rfc2732()" method in Template::Filters
use Template::Filters Template::Filters->use_rfc2732;
[% FILTER indent('ME> ') %] blah blah blah cabbages, rhubard, onions [% END %]
Output:
ME> blah blah blah ME> cabbages, rhubard, onions
[% FILTER truncate(21) %] I have much to say on this matter that has previously been said on more than one occasion. [% END %]
Output:
I have much to say...
If you want to use something other than '"..."' you can pass that as a second argument.
[% FILTER truncate(26, '…') %] I have much to say on this matter that has previously been said on more than one occasion. [% END %]
Output:
I have much to say…
[% FILTER repeat(3) %] We want more beer and we want more beer, [% END %] We are the more beer wanters!
Output:
We want more beer and we want more beer, We want more beer and we want more beer, We want more beer and we want more beer, We are the more beer wanters!
[% "The cat sat on the mat" FILTER remove('\s+') %]
Output:
Thecatsatonthemat
[% "The cat sat on the mat" | replace('\s+', '_') %]
Output:
The_cat_sat_on_the_mat
[% FOREACH user IN myorg.userlist %] [% FILTER redirect("users/${user.id}.html") %] [% INCLUDE userinfo %] [% END %] [% END %]
or more succinctly, using side-effect notation:
[% FOREACH user IN myorg.userlist; INCLUDE userinfo FILTER redirect("users/${user.id}.html"); END %]
A "file" exception will be thrown if the "OUTPUT_PATH" option is undefined.
An optional "binmode" argument can follow the filename to explicitly set the output file to binary mode.
[% PROCESS my/png/generator FILTER redirect("images/logo.png", binmode=1) %]
For backwards compatibility with earlier versions, a single true/false value can be used to set binary mode.
[% PROCESS my/png/generator FILTER redirect("images/logo.png", 1) %]
For the sake of future compatibility and clarity, if nothing else, we would strongly recommend you explicitly use the named "binmode" option as shown in the first example.
my $vars = { fragment => "The cat sat on the [% place %]", }; $template->process($file, $vars);
The following example:
[% fragment | eval %]
is therefore equivalent to
The cat sat on the [% place %]
The "evaltt" filter is provided as an alias for "eval".
[% my_perl_code | perl %]
In most cases, the "[% PERL %]" ... "[% END %]" block should suffice for evaluating Perl code, given that template directives are processed before being evaluate as Perl. Thus, the previous example could have been written in the more verbose form:
[% PERL %] [% my_perl_code %] [% END %]
as well as
[% FILTER perl %] [% my_perl_code %] [% END %]
The "evalperl" filter is provided as an alias for "perl" for backwards compatibility.
[% PROCESS something/cool FILTER stdout(binmode=1) # recommended %] [% PROCESS something/cool FILTER stdout(1) # alternate %]
The "stdout" filter can be used to force "binmode" on "STDOUT", or also inside "redirect", "null" or "stderr" blocks to make sure that particular output goes to "STDOUT". See the "null" filter below for an example.
[% FILTER null; USE im = GD.Image(100,100); black = im.colorAllocate(0, 0, 0); red = im.colorAllocate(255,0, 0); blue = im.colorAllocate(0, 0, 255); im.arc(50,50,95,75,0,360,blue); im.fill(50,50,red); im.png | stdout(1); END; -%]
Notice the use of the "stdout" filter to ensure that a particular expression generates output to "STDOUT" (in this case in binary mode).