O'Reilly logo

Perl Template Toolkit by David Cross, Andy Wardley, Darren Chamberlain

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 5. Filters

Filters are a powerful feature of the Template Toolkit that allow you to postprocess parts of the output of your template in many different ways. A number of filters for carrying out common tasks are included with the standard Template Toolkit distribution, and it is possible to extend this set by writing your own.

A good example of a filter that comes with the Template Toolkit is the html filter. In an HTML document, a number of characters have special meanings, so if you want these characters to appear in your document they need to be converted to HTML Entities. The html filter converts the characters <, >, “, and & to &lt;, &gt;, &quot;, and &amp;, respectively.[8]

Example 5-1 shows the html filter in action. Without the filter, the JavaScript section in the example would be treated as actual JavaScript code and executed. The filter converts the < characters, thereby changing the JavaScript to text that would be displayed by a browser rather than being executed.

Example 5-1. Filtering Javascript
<p>Here is what the JavaScript should look like:</p>
<pre>
[% FILTER html %]
<script language="JavaScript" type="text/javascript">
<!--
document.writeln("Hello, world");
//-->
</script> 
[% END %]
</pre>

The processed document looks like this:

<p>Here is what the JavaScript should look like:</p>
<pre>

&lt;script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;&gt;
&lt;!--
    document.writeln(&quot;Hello, world&quot;);
//--&gt;
&lt;/script&gt;
</pre>

This example ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required