Perl
  Home arrow Perl arrow Page 5 - 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 - Components and Macros
    ( Page 5 of 7 )

    When we looked at HTML::Mason, one of the things we praised was the ability to split template functionality up into multiple components, then include those components with particular parameters. It shouldn't be a surprise that we can do precisely the same in Template Toolkit.

    The mechanism through which we pull in components is the INCLUDE directive. For instance, we can specify our box drawing library in a way very similar to the HTML::Mason method, as in Example 3-16.

    Example 3-16. BoxTop

    <table bgcolor="#777777" cellspacing=0 border=0 cellpadding=0>
    <tr>
     
    <td rowspan=2></td>
     
    <td valign=middle align=left bgcolor="[% color %]">
        &nbsp;
       
    <font size=-1 color="#ffffff">
      
    <b>
       [% IF title_href %]
          <a href="[% title_href %]"> [% title %] </a>
       [% ELSE %]
         
    [% title %]
      
    [% END %]
      
    </b>
      
    </font>
     
    </td>
     
    <td rowspan=2>&nbsp;</td>
    </tr>
    <tr>
     
    <td colspan=2 bgcolor="#eeeeee" valign=top align=left width=100%>
      <table cellpadding=2 width=100%>
        <tr><td>

    And in the same way as HTML::Mason, we can use local parameters when we include these components:

      [% INCLUDE boxtop
                
    title = "Login"
                
    ...
      %]

    However, Template Toolkit provides another method of abstracting out common components, the MACRO directive. We can define a MACRO to expand to any Template Toolkit code; let's start by defining it to simply INCLUDE the drawing component:

      [% MACRO boxtop INCLUDE boxtop %]
     
    [% MACRO boxend INCLUDE boxend %]

    With this, we can draw boxes with a little less syntax:

      [% boxtop(title="My Box") %] 
         
    <P> Hello, people! </P>
      [% boxend %]

    Instead of using a component file and INCLUDE, we can also associate a block of Template Toolkit directives with a macro name.

      [% MACRO boxtop BLOCK %]
      <table bgcolor="#777777" cellspacing=0 border=0 cellpadding=0>
      <tr> 
       
    ...
      [% END %]

      [% MACRO boxend BLOCK %]
      </td></tr></table>

      </td></tr>
      <tr><td colspan=4>&nbsp;</td></tr>
      </table>
      [% END %]

    Eventually, we can build up a library of useful macros and then INCLUDE that, instead of having a bunch of component files hanging around.

    Let's assume we've created such a library and it contains these two box-drawing macros, and now we'll move on to putting together our RSS aggregator.

    The RSS Aggregator

    When it comes to writing the aggregator, we first look at the list of Template Toolkit plugins and notice with some delight that there's already a Template::Plugin::XML::RSS, which talks to XML::RSS. Unfortunately, our delight is short-lived, as we soon discover that this expects to get a filename rather than a URL or a string of XML data. We don't really want to be writing out files and then parsing them in again.

    Template Toolkit

    So let's create our own subclass of Template::Plugin::XML::RSS that fetches URLs and parses those instead:

      package Template::Plugin::XML::RSS::URL;
      use base 'Template::Plugin::XML::RSS';
      use LWP::Simple;

      sub new {
          my ($class, $context, $url) = @_;

          return $class->fail('No URL specified') unless $url;

          my $url_data = get($url)
            or return $class->fail("Couldn't fetch $url");

          my $rss = XML::RSS->new
            or return $class->fail('failed to create XML::RSS');

          eval { $rss->parse($url_data) } and not $@
            or return $class->fail("failed to parse $url: $@");

            return $rss;
      }

      1;

    Now we can build up the equivalent of the RSSBox component we made in Mason:

      [% MACRO RSSBox(url) USE rss = XML.RSS.URL(url) %]
      [% box_top(title = rss.channel.title, title_href = rss.channel.link) %]

      <dl class="rss">
      [% FOREACH item = news.items %]
          <dt class="rss">
             <a href="[% item.link %]"> [% item.title %] </a>
          [% IF full %]
            
    <dd> [% item.description %] </dd>
          [% END ]
          </dt>
     
    [% END %]
      </dl>
      [% box_end %]
      [% END %]

    The important difference between this and the Mason example is that this piece of code handles everything itself--the whole process of obtaining and parsing the RSS feed is available to the template designer. There's no Perl code here to be seen at all. It's also considerably more concise and easier to read and understand. Now that we have this macro, we can produce an HTML box full of RSS stories with a simple call to it:

      [% RSSBox("http://slashdot.org/slashdot.rss") %]

    From here on, constructing an RSS aggregator is a simple matter of templating; all of the Perl work has been abstracted away.



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek