Perl
  Home arrow Perl arrow Page 2 - More Templating Tools for Perl
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

More Templating Tools for Perl
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-09-04


    Table of Contents:
  • More Templating Tools for Perl
  • Template Toolkit
  • Filters
  • Plugins
  • Components and Macros
  • AxKit
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    More Templating Tools for Perl - Template Toolkit
    ( Page 2 of 7 )

    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 Articles          >>> More By O'Reilly Media
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek