Home arrow Perl Programming arrow Page 3 - More Templating Tools for Perl

Filters - Perl

In this conclusion to a five-part series on templating tools, you'll learn about filters, plugins, and more. It is excerpted from chapter three of the book Advanced Perl Programming, Second Edition, written by Simon Cozens (O'Reilly; ISBN: 0596004567). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. More Templating Tools for Perl
  2. Template Toolkit
  3. Filters
  4. Plugins
  5. Components and Macros
  6. AxKit
  7. Conclusion
By: O'Reilly Media
Rating: starstarstarstarstar / 3
September 04, 2008

print this article



Template Toolkit filters are a little like Unix filters--they're little routines that take an input, transform it, and spit it back out again. And just like Unix filters, they're connected to our template output with a pipe symbol (|).

In this case, the filter we want is the oddly named format filter, which performs printf-like formatting on its input:

  [% job.description | format("%60s") %] : [% job.cost %]

This fixes the case where the data is being produced by our template processor--job.description is turned into a real description, and then filtered. But we can also filter whole blocks of template content. For example, if we wanted to format the output as HTML, we could apply the html_entity filter to replace entities with their HTML encoding:

  [% FILTER html_entity %]
  Payment terms: < 30 days.
  [% END %]

This turns into:  Payment terms: &lt; 30 days.

This is another example of a Template Toolkit block; we've seen FOREACH blocks and FILTER blocks. There's also the IF/ ELSIF/ELSE block:

  [% IF delinquent %]
Our records indicate that this is the second issuing of this
  invoice. Please pay IMMEDIATELY.
  [% ELSE %]
Payment terms: <30 days.
  [% END %]

Other interesting filters include the upper, lower, ucfirst, and lcfirst filters to change the casing of the text; uri to URI-escape any special characters; eval to treat the text to another level of template processing, and perl_eval to treat the output as Perl, eval it, and then add the output to the template. For a more complete list of filters with examples, see the Template::Manual::Filters documentation.

>>> More Perl Programming Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: