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 <, >, ", and &, 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.
<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>
<script language="JavaScript" type="text/javascript">
<!--
document.writeln("Hello, world");
//-->
</script>
</pre>This example ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access