Home arrow Perl Programming arrow Page 4 - Building a Complete Website using the Template Toolkit

Loading the Configuration Template - Perl

See how the Template Toolkit simplifies the process of building and managing web site content. Examples illustrate the use of template variables and template components that allow web content to be constructed in a modular fashion. Other topics include managing the site structure, generating menus and other navigation components, and defining and using complex data. (From Perl Template Toolkit, Darren Chamberlain, Dave Cross, and Andy Wardley, O'Reilly Media, 2004, ISBN: 0596004761.)

  1. Building a Complete Website using the Template Toolkit
  2. A “Hello World” HTML Template
  3. Benefits of Modularity
  4. Loading the Configuration Template
  5. Creating a Project Directory
  6. A Place for Everything, and Everything in Its Place
  7. Adding Headers and Footers Automatically
  8. More Template Components
  9. Setting Default Values
  10. Wrapper and Layout Templates
  11. Using Layout Templates
  12. Menu Components
  13. Structured Configuration Templates
  14. Layered Configuration Templates
  15. Assessment
By: O'Reilly Media
Rating: starstarstarstarstar / 33
September 15, 2004

print this article



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

     Mostly Harmless.
[% 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


Home of the custom-made
luxury-planet building industry.

[% 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.

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