Home arrow Perl Programming arrow More Templating Tools for Perl

More Templating Tools for 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

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