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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PERL

More Templating Tools for Perl
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · This article is an excerpt from the book "Advanced Perl Programming, Second...
     

    Buy this book now. This article is excerpted from chapter three of the book Advanced Perl Programming, Second Edition, written by Simon Cozens (O'Reilly; ISBN: 0596004567). Check it out today at your favorite bookstore. Buy this book now.

       

    PERL ARTICLES

    - 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
    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT