.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Template::Manual::Filters 3" .TH Template::Manual::Filters 3 2023-07-25 "perl v5.38.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Template::Manual::Filters \- Standard filters .SH format(format) .IX Header "format(format)" The \f(CW\*(C`format\*(C'\fR filter takes a format string as a parameter (as per \&\f(CWprintf()\fR) and formats each line of text accordingly. .PP .Vb 4 \& [% FILTER format(\*(Aq\*(Aq) %] \& This is a block of text filtered \& through the above format. \& [% END %] .Ve .PP Output: .PP .Vb 2 \& \& .Ve .SH upper .IX Header "upper" Folds the input to UPPER CASE. .PP .Vb 1 \& [% "hello world" FILTER upper %] .Ve .PP Output: .PP .Vb 1 \& HELLO WORLD .Ve .SH lower .IX Header "lower" Folds the input to lower case. .PP .Vb 1 \& [% "Hello World" FILTER lower %] .Ve .PP Output: .PP .Vb 1 \& hello world .Ve .SH ucfirst .IX Header "ucfirst" Folds the first character of the input to UPPER CASE. .PP .Vb 1 \& [% "hello" FILTER ucfirst %] .Ve .PP Output: .PP .Vb 1 \& Hello .Ve .SH lcfirst .IX Header "lcfirst" Folds the first character of the input to lower case. .PP .Vb 1 \& [% "HELLO" FILTER lcfirst %] .Ve .PP Output: .PP .Vb 1 \& hELLO .Ve .SH trim .IX Header "trim" Trims any leading or trailing whitespace from the input text. Particularly useful in conjunction with \f(CW\*(C`INCLUDE\*(C'\fR, \f(CW\*(C`PROCESS\*(C'\fR, etc., having the same effect as the \f(CW\*(C`TRIM\*(C'\fR configuration option. .PP .Vb 1 \& [% INCLUDE myfile | trim %] .Ve .SH collapse .IX Header "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. .PP .Vb 1 \& [% FILTER collapse %] \& \& The cat \& \& sat on \& \& the mat \& \& [% END %] .Ve .PP Output: .PP .Vb 1 \& The cat sat on the mat .Ve .SH html .IX Header "html" Converts the characters \f(CW\*(C`<\*(C'\fR, \f(CW\*(C`>\*(C'\fR, \f(CW\*(C`&\*(C'\fR and \f(CW\*(C`"\*(C'\fR to \f(CW\*(C`<\*(C'\fR, \&\f(CW\*(C`>\*(C'\fR, \f(CW\*(C`&\*(C'\fR, and \f(CW\*(C`"\*(C'\fR respectively, protecting them from being interpreted as representing HTML tags or entities. .PP .Vb 3 \& [% FILTER html %] \& Binary "<=>" returns \-1, 0, or 1 depending on... \& [% END %] .Ve .PP Output: .PP .Vb 1 \& Binary "<=>" returns \-1, 0, or 1 depending on... .Ve .SH html_entity .IX Header "html_entity" The \f(CW\*(C`html\*(C'\fR filter is fast and simple but it doesn't encode the full range of HTML entities that your text may contain. The \f(CW\*(C`html_entity\*(C'\fR filter uses either the \f(CW\*(C`Apache::Util\*(C'\fR module (which is written in C and is therefore faster) or the \f(CW\*(C`HTML::Entities\*(C'\fR module (written in Perl but equally as comprehensive) to perform the encoding. .PP If one or other of these modules are installed on your system then the text will be encoded (via the \f(CWescape_html()\fR or \f(CWencode_entities()\fR subroutines respectively) to convert all extended characters into their appropriate HTML entities (e.g. converting '\f(CW\*(C`?\*(C'\fR' to '\f(CW\*(C`é\*(C'\fR'). If neither module is available on your system then an '\f(CW\*(C`html_entity\*(C'\fR' exception will be thrown reporting an appropriate message. .PP 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: \&\fBuse_html_entities()\fR or \&\fBuse_apache_util()\fR. .PP .Vb 2 \& use Template::Filters; \& Template::Filters\->use_html_entities; .Ve .PP For further information on HTML entity encoding, see . .SH xml .IX Header "xml" Same as the \f(CW\*(C`html\*(C'\fR filter, but adds \f(CW\*(C`'\*(C'\fR which is the fifth XML built-in entity. .SH html_para .IX Header "html_para" This filter formats a block of text into HTML paragraphs. A sequence of two or more newlines is used as the delimiter for paragraphs which are then wrapped in HTML \f(CW\*(C`

\*(C'\fR...\f(CW\*(C`

\*(C'\fR tags. .PP .Vb 2 \& [% FILTER html_para %] \& The cat sat on the mat. \& \& Mary had a little lamb. \& [% END %] .Ve .PP Output: .PP .Vb 3 \&

\& The cat sat on the mat. \&

\& \&

\& Mary had a little lamb. \&

.Ve .SH "html_break / html_para_break" .IX Header "html_break / html_para_break" Similar to the html_para filter described above, but uses the HTML tag sequence \f(CW\*(C`

\*(C'\fR to join paragraphs. .PP .Vb 2 \& [% FILTER html_break %] \& The cat sat on the mat. \& \& Mary had a little lamb. \& [% END %] .Ve .PP Output: .PP .Vb 4 \& The cat sat on the mat. \&
\&
\& Mary had a little lamb. .Ve .SH html_line_break .IX Header "html_line_break" This filter replaces any newlines with \f(CW\*(C`
\*(C'\fR HTML tags, thus preserving the line breaks of the original text in the HTML output. .PP .Vb 4 \& [% FILTER html_line_break %] \& The cat sat on the mat. \& Mary had a little lamb. \& [% END %] .Ve .PP Output: .PP .Vb 2 \& The cat sat on the mat.
\& Mary had a little lamb.
.Ve .SH uri .IX Header "uri" This filter URI escapes the input text, converting any characters outside of the permitted URI character set (as defined by RFC 3986) into a \f(CW%nn\fR hex escape. .PP .Vb 1 \& [% \*(Aqmy file.html\*(Aq | uri %] .Ve .PP Output: .PP .Vb 1 \& my%20file.html .Ve .PP The uri filter correctly encodes all reserved characters, including \&\f(CW\*(C`&\*(C'\fR, \f(CW\*(C`@\*(C'\fR, \f(CW\*(C`/\*(C'\fR, \f(CW\*(C`;\*(C'\fR, \f(CW\*(C`:\*(C'\fR, \f(CW\*(C`=\*(C'\fR, \f(CW\*(C`+\*(C'\fR, \f(CW\*(C`?\*(C'\fR and \f(CW\*(C`$\*(C'\fR. 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: .PP .Vb 5 \& [% path = \*(Aqhttp://tt2.org/example\*(Aq \& back = \*(Aq/other?foo=bar&baz=bam\*(Aq \& title = \*(AqEarth: "Mostly Harmless"\*(Aq \& %] \& .Ve .PP The output generated is rather long so we'll show it split across two lines: .PP .Vb 2 \& .Ve .PP Without the uri filter the output would look like this (also split across two lines). .PP .Vb 2 \& .Ve .PP In this rather contrived example we've manage to generate both a broken URL (the repeated \f(CW\*(C`?\*(C'\fR is not allowed) and a broken HTML element (the href attribute is terminated by the first \f(CW\*(C`"\*(C'\fR after \f(CW\*(C`Earth: \*(C'\fR leaving \f(CW\*(C`Mostly Harmless"\*(C'\fR 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. .PP However, you should \fBnot\fR use the uri filter to encode an entire URL. .PP .Vb 1 \& # WRONG! .Ve .PP This will incorrectly encode any reserved characters like \f(CW\*(C`:\*(C'\fR and \f(CW\*(C`/\*(C'\fR and that's almost certainly not what you want in this case. Instead you should use the \fBurl\fR (note spelling) filter for this purpose. .PP .Vb 1 \& # CORRECT .Ve .PP 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. .PP As of version 2.28 of the Template Toolkit, the \f(CW\*(C`uri\*(C'\fR and url filters use the unsafe character set defined by RFC3986. This means that certain characters ("(", ")", "*", "!", "'", and '"') are now deemed unsafe and will be escaped as hex character sequences. .PP The ability to use the RFC3986 character set was added in 2.26 but not enabled by default; double quote was incorrectly deemed safe in 2.26 but correctly escaped in 2.27. .PP If you want to enable the old behaviour then call the \f(CWuse_rfc2732()\fR method in Template::Filters .PP .Vb 2 \& use Template::Filters \& Template::Filters\->use_rfc2732; .Ve .SH url .IX Header "url" The url filter is a less aggressive version of the uri filter. It encodes any characters outside of the permitted URI character set (as defined by RFC 2396) into \f(CW%nn\fR hex escapes. However, unlike the uri filter, the url filter does \&\fBnot\fR encode the reserved characters \f(CW\*(C`&\*(C'\fR, \f(CW\*(C`@\*(C'\fR, \f(CW\*(C`/\*(C'\fR, \f(CW\*(C`;\*(C'\fR, \f(CW\*(C`:\*(C'\fR, \f(CW\*(C`=\*(C'\fR, \f(CW\*(C`+\*(C'\fR, \&\f(CW\*(C`?\*(C'\fR and \f(CW\*(C`$\*(C'\fR. .SH indent(pad) .IX Header "indent(pad)" Indents the text block by a fixed pad string or width. The '\f(CW\*(C`pad\*(C'\fR' argument can be specified as a string, or as a numerical value to indicate a pad width (spaces). Defaults to 4 spaces if unspecified. .PP .Vb 4 \& [% FILTER indent(\*(AqME> \*(Aq) %] \& blah blah blah \& cabbages, rhubard, onions \& [% END %] .Ve .PP Output: .PP .Vb 2 \& ME> blah blah blah \& ME> cabbages, rhubard, onions .Ve .SH truncate(length,dots) .IX Header "truncate(length,dots)" Truncates the text block to the length specified, or a default length of 32. Truncated text will be terminated with '\f(CW\*(C`...\*(C'\fR' (i.e. the '\f(CW\*(C`...\*(C'\fR' falls inside the required length, rather than appending to it). .PP .Vb 4 \& [% FILTER truncate(21) %] \& I have much to say on this matter that has previously \& been said on more than one occasion. \& [% END %] .Ve .PP Output: .PP .Vb 1 \& I have much to say... .Ve .PP If you want to use something other than '\f(CW\*(C`...\*(C'\fR' you can pass that as a second argument. .PP .Vb 4 \& [% FILTER truncate(26, \*(Aq…\*(Aq) %] \& I have much to say on this matter that has previously \& been said on more than one occasion. \& [% END %] .Ve .PP Output: .PP .Vb 1 \& I have much to say… .Ve .SH repeat(iterations) .IX Header "repeat(iterations)" Repeats the text block for as many iterations as are specified (default: 1). .PP .Vb 4 \& [% FILTER repeat(3) %] \& We want more beer and we want more beer, \& [% END %] \& We are the more beer wanters! .Ve .PP Output: .PP .Vb 4 \& 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! .Ve .SH remove(string) .IX Header "remove(string)" Searches the input text for any occurrences of the specified string and removes them. A Perl regular expression may be specified as the search string. .PP .Vb 1 \& [% "The cat sat on the mat" FILTER remove(\*(Aq\es+\*(Aq) %] .Ve .PP Output: .PP .Vb 1 \& Thecatsatonthemat .Ve .SH "replace(search, replace)" .IX Header "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. .PP .Vb 1 \& [% "The cat sat on the mat" | replace(\*(Aq\es+\*(Aq, \*(Aq_\*(Aq) %] .Ve .PP Output: .PP .Vb 1 \& The_cat_sat_on_the_mat .Ve .SH "redirect(file, options)" .IX Header "redirect(file, options)" The \f(CW\*(C`redirect\*(C'\fR filter redirects the output of the block into a separate file, specified relative to the \f(CW\*(C`OUTPUT_PATH\*(C'\fR configuration item. .PP .Vb 5 \& [% FOREACH user IN myorg.userlist %] \& [% FILTER redirect("users/${user.id}.html") %] \& [% INCLUDE userinfo %] \& [% END %] \& [% END %] .Ve .PP or more succinctly, using side-effect notation: .PP .Vb 5 \& [% FOREACH user IN myorg.userlist; \& INCLUDE userinfo \& FILTER redirect("users/${user.id}.html"); \& END \& %] .Ve .PP A \f(CW\*(C`file\*(C'\fR exception will be thrown if the \f(CW\*(C`OUTPUT_PATH\*(C'\fR option is undefined. .PP An optional \f(CW\*(C`binmode\*(C'\fR argument can follow the filename to explicitly set the output file to binary mode. .PP .Vb 2 \& [% PROCESS my/png/generator \& FILTER redirect("images/logo.png", binmode=1) %] .Ve .PP For backwards compatibility with earlier versions, a single true/false value can be used to set binary mode. .PP .Vb 2 \& [% PROCESS my/png/generator \& FILTER redirect("images/logo.png", 1) %] .Ve .PP For the sake of future compatibility and clarity, if nothing else, we would strongly recommend you explicitly use the named \f(CW\*(C`binmode\*(C'\fR option as shown in the first example. .SH "eval / evaltt" .IX Header "eval / evaltt" The \f(CW\*(C`eval\*(C'\fR 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 fragments from an external source such as a database, which can then be processed in the template as required. .PP .Vb 4 \& my $vars = { \& fragment => "The cat sat on the [% place %]", \& }; \& $template\->process($file, $vars); .Ve .PP The following example: .PP .Vb 1 \& [% fragment | eval %] .Ve .PP is therefore equivalent to .PP .Vb 1 \& The cat sat on the [% place %] .Ve .PP The \f(CW\*(C`evaltt\*(C'\fR filter is provided as an alias for \f(CW\*(C`eval\*(C'\fR. .SH "perl / evalperl" .IX Header "perl / evalperl" The \f(CW\*(C`perl\*(C'\fR filter evaluates the block as Perl code. The \f(CW\*(C`EVAL_PERL\*(C'\fR option must be set to a true value or a \f(CW\*(C`perl\*(C'\fR exception will be thrown. .PP .Vb 1 \& [% my_perl_code | perl %] .Ve .PP In most cases, the \f(CW\*(C`[% PERL %]\*(C'\fR ... \f(CW\*(C`[% END %]\*(C'\fR 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: .PP .Vb 3 \& [% PERL %] \& [% my_perl_code %] \& [% END %] .Ve .PP as well as .PP .Vb 3 \& [% FILTER perl %] \& [% my_perl_code %] \& [% END %] .Ve .PP The \f(CW\*(C`evalperl\*(C'\fR filter is provided as an alias for \f(CW\*(C`perl\*(C'\fR for backwards compatibility. .SH stdout(options) .IX Header "stdout(options)" The stdout filter prints the output generated by the enclosing block to \&\f(CW\*(C`STDOUT\*(C'\fR. The \f(CW\*(C`binmode\*(C'\fR option can be passed as either a named parameter or a single argument to set \f(CW\*(C`STDOUT\*(C'\fR to binary mode (see the binmode perl function). .PP .Vb 2 \& [% PROCESS something/cool \& FILTER stdout(binmode=1) # recommended %] \& \& [% PROCESS something/cool \& FILTER stdout(1) # alternate %] .Ve .PP The \f(CW\*(C`stdout\*(C'\fR filter can be used to force \f(CW\*(C`binmode\*(C'\fR on \f(CW\*(C`STDOUT\*(C'\fR, or also inside \f(CW\*(C`redirect\*(C'\fR, \f(CW\*(C`null\*(C'\fR or \f(CW\*(C`stderr\*(C'\fR blocks to make sure that particular output goes to \f(CW\*(C`STDOUT\*(C'\fR. See the \f(CW\*(C`null\*(C'\fR filter below for an example. .SH stderr .IX Header "stderr" The stderr filter prints the output generated by the enclosing block to \&\f(CW\*(C`STDERR\*(C'\fR. .SH null .IX Header "null" The \f(CW\*(C`null\*(C'\fR filter prints nothing. This is useful for plugins 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: .PP .Vb 10 \& [% 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; \& \-%] .Ve .PP Notice the use of the \f(CW\*(C`stdout\*(C'\fR filter to ensure that a particular expression generates output to \f(CW\*(C`STDOUT\*(C'\fR (in this case in binary mode).