Perl
  Home arrow Perl arrow Page 4 - Site Layout with Perl Templating Tools
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

Site Layout with Perl Templating Tools
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-08-28


    Table of Contents:
  • Site Layout with Perl Templating Tools
  • Building the Index
  • Helper Components
  • Site Layout

  • 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


    Site Layout with Perl Templating Tools - Site Layout
    ( Page 4 of 4 )

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

    <ul>
    <%$Portal::dirs%>
    </ul>
    <& /BoxEnd &>

    <%ONCE>
        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/;
        }
    </%ONCE>

    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

    <%ARGS>
    $open
    </%ARGS>

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

      01-Register
     
    02-Slashdot
     
    03-MacNews
     
    04-LinuxToday
     
    05-PerlDotCom

    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
    page.
    <& /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/
    60/60.xml",
      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 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek