Perl
  Home arrow Perl arrow Page 4 - Building a Complete Website using the Template Toolkit
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? 
PERL

Building a Complete Website using the Template Toolkit
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 31
    2004-09-15


    Table of Contents:
  • Building a Complete Website using the Template Toolkit
  • A “Hello World” HTML Template
  • Benefits of Modularity
  • Loading the Configuration Template
  • Creating a Project Directory
  • A Place for Everything, and Everything in Its Place
  • Adding Headers and Footers Automatically
  • More Template Components
  • Setting Default Values
  • Wrapper and Layout Templates
  • Using Layout Templates
  • Menu Components
  • Structured Configuration Templates
  • Layered Configuration Templates
  • Assessment

  • 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


    Building a Complete Website using the Template Toolkit - Loading the Configuration Template
    ( Page 4 of 15 )

    The config template can now be loaded using the PROCESS directive to gain access to these variable definitions. This is shown in Example 2-8, which also defines the title variable specific to this page. This is really no different from the way you might define a constant or global variable at the start of a program in Perl or some other programming language. It’s good practice to do this at the top of the file, where any future changes can easily be made.

    Example 2-8. earth.tt

    [% title = 'Earth' -%]
    [% PROCESS config -%]
    [% PROCESS header %]

       <p>
         Mostly Harmless.
       </p>
    [% PROCESS footer %]

    Notice the - character placed immediately before the closing %] tags at the end of the directives on the first two lines. This tells the Template Toolkit to remove, or chomp, the newline and any other whitespace following the directive. Some older web browsers don’t like to see whitespace appearing before the opening element, so this ensures that the header file is inserted right at the top of the output. In effect, it is as if we had written the template like so:

    [% title = 'Earth' %][% PROCESS config %][% PROCESS header %] ...

    Now the template can be processed using tpage without the need to provide variable values as command-line arguments:

    $ tpage earth.tt > earth.html

    Merging directives

    The start of each page template can be simplified by defining the title variable and the PROCESS directives within a single directive tag. Each command is separated from the next by a ; (semicolon) character.

    For example, we can write:

    [% title = 'Earth';
       PROCESS config;
       PROCESS header
    %]

    instead of the more verbose:

    [% title = 'Earth' -%]
    [% PROCESS config -%]
    [% PROCESS header %]

    There’s no need for a semicolon at the end of the last directive, but the Template Toolkit won’t complain if it finds one there. As we saw earlier, semicolons aren’t required between variable definitions that appear one after another. However, a semicolon is required if you switch from setting variables (which is technically the SET directive, although the explicit keyword is rarely used) to another kind of direc tive (e.g., PROCESS) in the same tag:

    [% pi = 3.142       # semicolon optional
        e = 2.718       # " " " "
        i = 1.414;      # semicolon mandatory
        PROCESS config; # " " " "
        phi = 1.618     # semicolon optional
    %]

    The distinction becomes a little more obvious when we use the SET keyword explicitly and add some whitespace to format the directives more clearly:

    [% SET pi = 3.14 2
            e = 2.718
            i = 1.414;

       PROCESS config;

       SET phi = 1.618
    %]

    There’s one final improvement we can make to the block at the start of our page templates. The two PROCESS

    directives can be merged into one, with the names of the templates separated by a + character:

    [% title = 'Earth';

       PROCESS config
             + header
    %]

    The general rule of whitespace being insignificant inside directives applies equally well to the PROCESS

    directive, allowing us to list all the files on the same line, or across a number of lines, as we’ve done here. This flexibility allows us to lay out this header block in such a way that it’s clear from a glance what’s going on, and with the bare minimum of extra syntax cluttering up this high-level view.

    Example 2-9 shows this in the context of a complete page template.

    Example 2-9. magrethea.tt

    [% title = 'Magrethea';

    PROCESS config
    + header

    -%]

    <p>
    Home of the custom-made
    luxury-planet building industry.
    </p>

    [% PROCESS footer %]

    Generating Many Pages

    The tpage program is fine for processing single templates, but isn’t really designed to handle the many pages that comprise a typical web site. For this, ttree is much more appropriate. It works by drilling down through a source directory of your choosing, looking for templates to process. The output generated is saved in a corresponding file in a separate destination directory.

    In addition to working well with a large number of template files, ttree also provides a much greater range of configuration options that allow you to modify the behavior of the Template Toolkit when processing templates. This allows you to further sim plify the process of generating and maintaining web content in a number of interesting ways that we’ll explore throughout this section.

    Our templates will need to be organized a little more carefully when using ttree.In particular, we need to separate those page templates that represent complete HTML pages (hello.tt, goodbye.tt, earth.tt, and magrethea.tt in our previous examples) from those that are reusable template components (config, header, and footer). 

    Buy the book! If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

       

    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
    Stay green...Green IT