Perl
  Home arrow Perl arrow 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
    ( Page 1 of 7 )

    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.

    Completing the Portal

    But first, let's complete our portal by writing the RSSBox library that all these sources use. First, we want a ONCE block to load up the modules we need:

      <%ONCE>
     
    use XML::RSS;
     
    use LWP::Simple;
     
    </%ONCE>

    Next we take our arguments, setting appropriate defaults:

      <%ARGS>
      $URL
      $Color => "#0000aa"
      $Max => 5
      $Full => 1
     
    $Title => undef
      </%ARGS>

    Before we start outputting any content, we load up the feed in question and parse it with the XML::RSS module. We call Mason's cache_self method to have this component handle caching its output; if the same URL is accessed within 10 minutes, the cached copy will be presented instead:

      <%INIT>
      return if $m->cache_self(key => $URL, expires_in => '10 minutes');
      my $rss = new XML::RSS;
      eval { $rss->parse(get($URL));};
      my $title = $Title || $rss->channel('title');
      </%INIT>

    And now we are ready to go. So lets look at this altogether in Example 3-15.

    Example 3-15. RSSBox

    <%ONCE>
    use XML::RSS;
    use LWP::Simple;
    </%ONCE>

    <%ARGS>
    $URL
    $Color => "#0000aa"
    $Max => 5
    $Full => 1
    $Title => undef
    </%ARGS>

    <%INIT>
    my $rss = new XML::RSS;
    eval { $rss->parse(get($URL));};
    my $title = $Title || $rss->channel('title');
    my $site = $rss->channel('link');
    </%INIT>

    <BR>
    <& BoxTop, color => $Color, title => $title, title_href => $site &>

        <dl class="rss">
    % my $count = 0;
    % for (@{$rss->{items}}) {
       
    <dt class="rss">
        <a href="<% $_->{link} %>"> <% $_->{title} %> </a>
        </dt>
    % if ($Full) {
        <dd> <% $_->{description} %> </dd>
    % }

    % last if ++$count >= $Max;
    % }
       
    </dl>
    <& /BoxEnd &>

    There isn't much to it; for each item in the feed, we want to provide a link, the item's title, and, optionally, the description. We stop if we have more items than we want.

    This demonstrates how powerful Mason can be; as I said, the total development time for this site was a couple of hours at most. The entire site takes considerably fewer than 200 lines of code. And, as we mentioned, we have the flexibility to include components that are not RSS. For instance, we don't actually have an RSS feed of the Oxford weather. However, there is a web page that spits out a weather report in a well-known format. This means that Weather/01-Oxford does not call RSSBox at all, but is in fact the following:

      <%INIT>
      use LWP::Simple;
      my @lines = grep /Temperature|Pressure|humidity|^Sun|Rain/,
                  split /\n/,
                  get('http://www-atm.physics.ox.ac.uk/user/cfinlay/now.htm');
      </%INIT>

      <br>
      <& /BoxTop, title => "Oxford Weather", color => "#dd00dd" &>

      <ul>
      % for (@lines) {
      
    <li> <% $_ %> </li>
      % }
      </ul>

      <& /BoxEnd &>

    And that sums up Mason--simple, extensible, and highly powerful.

    Of course, there are many other Mason tricks for you to learn--too many to cover here. Dave Rolsky and Ken Williams's fantastic book Embedding Perl in HTML with Mason (http://www.masonbook.com/) covers many of them, including more details about getting Mason up and running in your web server. Also check out the Mason home page (http://www.masonhq.com).



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