Home arrow Perl Programming arrow Page 4 - Site Layout with Perl Templating Tools

Site Layout - Perl

In this fourth part of a five-part series on templating tools, you'll learn about Perl blocks, helper components, 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.

  1. Site Layout with Perl Templating Tools
  2. Building the Index
  3. Helper Components
  4. Site Layout
By: O'Reilly Media
Rating: starstarstarstarstar / 3
August 28, 2008

print this article



Now we need to make some decisions about our site's layout. As we've mentioned, we're going to put our feeds in the filesystem, categorized by directory. Well actually have each individual feed be a Mason component, drawing on a library component we'll call RSSBox. Our Directories component is a box containing a list of categories; clicking on a category displays all the feeds in that category. As each category is a directory, we can create the list, as in Example 3-12.

Example 3-12. Directories

<& /BoxTop, title=> "Resources" &>

<& /BoxEnd &>

    my $root = "/var/portal/";
    for my $top (grep { -d $_ } glob("$root*")) {
$top =~ s/$root//;
        $Portal::dirs .= qq{
           <li><a href="/?open=$top">$top</a>
        } unless $top =~ /\W/;

What's happening here is that when the server starts up, it looks at all the subdirectories of our portal directory and strips them of their root (in this instance, /var/portal/)to turn them into a link for the purposes of our application. For instance, a directory called /var/portal/News would turn into a link
/?open=News with the heading News. This link redirects back to our home page, where the open parameter causes the DirectoryPane to be presented and opens the feeds in the selected directory. The code skips any directories with non-word characters in the name, so it only generates links that will pass the parameter check on open.

Let's think about how that pane is implemented. We know that we open a directory and find it full of Mason component files. We want to then dynamically include each of those component files in turn, to build up our directory of feeds.

The trick to dynamically calling a component is the comp method on the Mason request object $m; this is the Perl-side version of the <& comp &> component include tag. Hence, our directory pane ends up looking like Example 3-13.

Example 3-13 . DirectoryPane


% for (grep {-f $_} glob( "/var/portal/$open/*") ) {
% s|/var/portal/||;

<% $m->comp($_) %>
% }

We first receive the name of the directory we're trying to open. Next we look at each file in that directory, strip off the name of the root directory (ideally this would all be provided by a configuration file), and then call the component with that name. This means that if we have a directory called Technology containing the following files:


then calling <& /DirectoryPane, open => "Technology" &> would have the effect of saying:

  <& /Technology/01-Register   &>
  <& /Technology/02-Slashdot   &>
  <& /Technology/03-MacNews    &>
<& /Technology/04-LinuxToday &>
  <& /Technology/05-PerlDotCom &>

The standard pane, shown in Example 3-14, appears when no directory is open. It consists of whatever feeds we choose to make default.

Example 3-14. StandardPane

<& /BoxTop, title=> "Hello!", color => "dd2222"&>
Welcome to your portal! From here you can subscribe to a wide range of
news and alerting services; if you log in, you can customize this home
<& /BoxEnd &>

<& /Weather/01-Oxford &>
<& /Technology/02-Slashdot &>
<& /News/01-BBC &>
<& /People/03-Rael &>

So what's in the individual files? As we've mentioned, they make use of an RSSBox component, and they simply pass in the URL for the feed and optionally a color, a maximum number of items, and a name for the feed. They also pass in a parameter to say whether we want to display just the titles and links for each RSS item, or the description as well. For instance, /News/01-BBC looks like this:

  <& /RSSBox, URL =>"http://www.newsisfree.com/HPE/xml/feeds/
  Color =>"#dd0000" &>

whereas Rael Dornfest's blog looks like this:

  <& /RSSBox, URL => http://www.oreillynet.com/~rael/index.rss,
  Color=> "#cccc00", Title => "Rael Dornfest", Full => 0 &>

As we'll see in a moment, the beauty of this modular system is that we can have components that do things other than fire off RSS feeds if we want.

Please check back next week for the conclusion to this article.

>>> More Perl Programming Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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