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

Template Toolkit - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

While the solutions we've seen so far have been primarily for Perl programmers--embedding Perl code in some other medium--Andy Wardley's Template Toolkit (http://www.template-toolkit.org/) is slightly different. It uses its own templating language to express components, loops, method calls, data structure elements, and more; it's therefore useful for teaching to designers who have no knowledge of the Perl side of your application* but who need to work on the presentation. As the documentation puts it, you should think of the Template Toolkit language as a set of layout directives for displaying data, not calculating it.

Like Mason, it seamlessly handles compiling, caching, and delivering your templates. However, unlike Mason, it's designed to provide general-purpose display and formatting capabilities in a very extensible way. As an example, you can use Template Toolkit to dynamically serve up PDF documents containing graphs based on data from a database--and all this using nothing other than the standard plugins and filters and all within the Template Toolkit mini language.

But before we look at the clever stuff, let's look at the very simple uses of Template Toolkit. In the simplest cases, it behaves a lot like Text::Template. We take a template object, feed it some values, and give it a template to process:

  use Template;
  my $template = Template->new();
  my $variables = {

      who        => "Andy Wardley",
      modulename => "Template Toolkit",
      hours      => 30,
      games      => int(30*2.4)
 
};
  $template->process("thankyou.txt", $variables);

This time, our template looks like the following:

  Dear [% who %],
     
Thank you for the [% modulename %] Perl module, which has saved me
  [% hours %] hours of work this year. This would have left me free to play
  [% games %] games of go, which I would have greatly appreciated
  had I not spent the time goofing off on IRC instead.

  Love,
  Simon

Lo and behold, the templated text appears on standard output. Notice, however, that our variables inside the [% and %] delimiters aren't Perl variables with the usual type sign in front of them; instead, they're now Template Toolkit variables. Template Toolkit variables can be more than just simple scalars, though; complex data structures and even Perl objects are available to Template Toolkit through a simple, consistent syntax. Let's go back to our design work invoices, but with a slightly different data structure:

  my $invoice = {
      client => "Acme Motorhomes and Eugenics Ltd.",
      jobs => [
      
{ cost => 450.00, description => "Designing the new logo" },
       { cost => 300.00, description => "Letterheads and complements slips" },

       { cost => 900.00, description => "Web site redesign" },
      
{ cost =>  33.75, description => "Miscellaneous Expenses" }
      
],
     
total => 0
  };

  $invoice->{total} += $_->{cost} for @{$invoice->{jobs}};

How would we design a template to fit that data? Obviously, we're going to need to loop over the jobs in the anonymous array and extract various hash values. Here's how it's done:

  To [% client %]:

  Thank you for consulting the services of Fungly Foobar Design
  Associates. Here is our invoice in accordance with the work we have
  carried out for you:

  [% FOREACH job = jobs %]
      
[% job.description %] : [% job.cost %]
  [% END %]

  Total                         $[% total %]

  Payment terms 30 days.

  Many thanks,
 
Fungly Foobar

As you can see, the syntax is inspired by Perl--we can foreach over a list and use a local variable job to represent each element of the iterator. The dot operator is equivalent to Perl's ->-- it dereferences array and hash reference elements and can also call methods on objects.

However, there's something slightly wrong with this example; since we can expect our descriptions to be of variable width, our costs aren't going to line up nicely at the end.* What can we do about this? This is where a nice, extensible feature of the Template Toolkit called filters comes in.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- 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: