filters(3)

NAME

Template::Manual::Filters - Standard filters

DESCRIPTION

This section lists all the standard filters distributed
with the Template Toolkit for post-processing output.

STANDARD FILTERS

format(format)

The 'format' filter takes a format string as a parameter
(as per printf()) and formats each line of text accord ingly.
[% 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. -->
upper
Folds the input to UPPER CASE.

[% "hello world" FILTER upper %]
output:

HELLO WORLD
lower
Folds the input to lower case.

[% "Hello World" FILTER lower %]
output:

hello world
ucfirst
Folds the first character of the input to UPPER CASE.

[% "hello" FILTER ucfirst %]
output:

Hello
lcfirst
Folds the first character of the input to lower case.

[% "HELLO" FILTER lcfirst %]
output:

hELLO
trim
Trims any leading or trailing whitespace from the input
text. Particularly useful in conjunction with INCLUDE,
PROCESS, etc., having the same effect as the TRIM configu
ration option.

[% INCLUDE myfile | trim %]
collapse
Collapse any whitespace sequences in the input text into a
single space. Leading and trailing whitespace (which
would be reduced to a single space) is removed, as per
trim.

[% FILTER collapse %]

The cat
sat on
the mat
[% END %]
output:

The cat sat on the mat
html
Converts the characters '<', '>' and '&' to '&lt;', '&gt;'
and '&amp;', respectively, protecting them from being
interpreted as representing HTML tags or entities.

[% FILTER html %]
Binary "<=>" returns -1, 0, or 1 depending on...
[% END %]
output:

Binary "&lt;=&gt;" returns -1, 0, or 1 depending on...
html_entity
The html filter is fast and simple but it doesn't encode
the full range of HTML entities that your text may con
tain. The html_entity filter uses either the Apache::Util
module (which is written in C and is therefore faster) or
the HTML::Entities module (written in Perl but equally as
comprehensive) to perform the encoding. 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 '&eacute;'). If neither module
is available on your system then an 'html_all' exception
will be thrown reporting an appropriate message.
For further information on HTML entity encoding, see
http://www.w3.org/TR/REC-html40/sgml/entities.html.
html_para
This filter formats a block of text into HTML paragraphs.
A sequence of two or more newlines is used as the delim
iter for paragraphs which are then wrapped in HTML
<p>...</p> tags.

[% 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>
html_break
Similar to the html_para filter described above, but uses
the HTML tag sequence <br><br> to join paragraphs.

[% 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.
uri
This filter URI escapes the input text, converting any
characters outside of the permitted URI character set (as
defined by RFC 2396) into a %nn hex escape.

[% 'my file.html' | uri %]
output:

my%20file.html
Note that URI escaping isn't always enough when generating
hyperlinks in an HTML document. The "&" character, for
example, is valid in a URI and will not be escaped by the
URI filter. In this case you should also filter the text
through the 'html' filter.

<a href="[% filename | uri | html %]">click here</a>
indent(pad)
Indents the text block by a fixed pad string or width.
The 'pad' argument can be specified as a string, or as a
numerical value to indicate a pad width (spaces).
Defaults to 4 spaces if unspecified.

[% FILTER indent('ME> ') %]
blah blah blah
cabbages, rhubard, onions
[% END %]
output:

ME> blah blah blah
ME> cabbages, rhubard, onions
truncate(length)
Truncates the text block to the length specified, or a
default length of 32. Truncated text will be terminated
with '...' (i.e. the '...' falls inside the required
length, rather than appending to it).

[% 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...
repeat(iterations)
Repeats the text block for as many iterations as are spec
ified (default: 1).

[% 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!
remove(string)
Searches the input text for any occurrences of the speci
fied string and removes them. A Perl regular expression
may be specified as the search string.

[% "The cat sat on the mat" FILTER remove(' %]
output:

Thecatsatonthemat
replace(search, replace)
Similar to the remove filter described above, but taking a
second parameter which is used as a replacement string for
instances of the search string.

[% "The cat sat on the mat" | replace(' '_') %]
output:

The_cat_sat_on_the_mat
redirect(file)
The 'redirect' filter redirects the output of the block
into a separate file, specified relative to the OUT
PUT_PATH configuration item.

[% FOREACH user = myorg.userlist %]
[% FILTER redirect("users/${user.id}.html") %]
[% INCLUDE userinfo %]
[% END %]
[% END %]
or more succinctly, using side-effect notation:

[% INCLUDE userinfo
FILTER redirect("users/${user.id}.html")
FOREACH user = myorg.userlist
%]
A 'file' exception will be thrown if the OUTPUT_PATH
option is undefined.
eval(template_text)
The 'eval' filter evaluates the block as template text,
processing any directives embedded within it. This allows
template variables to contain template fragments, or for
some method to be provided for returning template frag
ments from an external source such as a database, which
can then be processed in the template as required.

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'.
perl(perlcode)
The 'perl' filter evaluates the block as Perl code. The
EVAL_PERL option must be set to a true value or a 'perl'
exception will be thrown.

[% 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.
stdout(binmode)
The stdout filter prints the output generated by the
enclosing block to STDOUT. If binmode is set, binary mode
on STDOUT is turned on (see the binmode perl function.
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.
stderr
The stderr filter prints the output generated by the
enclosing block to STDERR.
null
The null filter prints nothing. This is useful for plug
ins whose methods return values that you don't want to
appear in the output. Rather than assigning every plugin
method call to a dummy variable to silence it, you can
wrap the block in a null filter:

[% 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 par
ticular expression generates output to stdout (in this
case in binary mode).
latex(outputType)
Passes the text block to LaTeX and produces either PDF,
DVI or PostScript output. The 'outputType' argument
determines the output format and it should be set to one
of the strings: "pdf" (default), "dvi", or "ps".
The text block should be a complete LaTeX source file.
g
i% FILTER latex("pdf") -%]
ncumentclass{article}
{
dnt}
o
c
u
m
itle{A Sample TT2 eX Source File}
uthor{Craig Barratt}
ketitle
tion{Introduction}
This is some text.
0{document}
[% END -%]
The output will be a PDF file. You should be careful not
to prepend or append any extraneous characters or text
outside the FILTER block, since this text will wrap the
(binary) output of the latex filter. Notice the END
directive uses '-%]' for the END_TAG to remove the trail
ing new line.
One example where you might prepend text is in a CGI
script where you might include the Content-Type before the
latex output, eg:

gontent-Type: application/pdf
i
n% FILTER latex("pdf") -%]
{cumentclass{article}
dnt}
...
0{document}
[% END -%]
m
In other cases you might use the redirect filter to put
the output into a file, rather than delivering it to std
out.g This might be suitable for batch scripts:
i
n% output = FILTER latex("pdf") -%]
{cumentclass{article}
dnt}
...
0{document}
[% END; output | redirect("document.pdf", 1) -%]
m
(Notice the second argument to redirect to force binary
mode.)
Note that the latex filter runs one or two external pro
grams, so it isn't very fast. But for modest documents
the performance is adequate, even for interactive applica
tions.
A error of type 'latex' will be thrown if there is an
error reported by latex, pdflatex or dvips.

AUTHOR

Andy Wardley <abw@andywardley.com>

<http://www.andywardley.com/|http://www.andywardley.com/>

VERSION

Template Toolkit version 2.08, released on 30 July 2002.

COPYRIGHT

Copyright (C) 1996-2002 Andy Wardley. All Rights Re
served.
Copyright (C) 1998-2002 Canon Research Centre Europe
Ltd.
Copyright © 2010-2025 Platon Technologies, s.r.o.           Home | Man pages | tLDP | Documents | Utilities | About
Design by styleshout