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

A Place for Everything, and Everything in Its Place - 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



Before we can run the build script to generate the site content, we will need to move our page and library template files into place.

The source templates for the HTML pages should now be moved into the src direc tory where ttree can find them. The HTML files that ttree generates in the html output directory will be given the same filename as the src template from which they are generated. For this reason, we’ll be using a .html file extension on our page templates from now on.

Also, move the template components config, header, and footer into the lib directory. These are (for now) also identical to those shown in the earlier examples.

Running the Build Script

Now we can run the bin/build script to invoke ttree to build the site content:

$ bin/build
ttree 2.63 (Template Toolkit version 2.10)

      Source: /home/dent/web/src
 Destination: /home/dent/web/html
Include Path: [ /home/dent/web/lib ]
      Ignore: [ \b(CVS|RCS)\b, ^# ]
        Copy: [ \.(png|gif|jpg)$ ]
      Accept: [ * ]

+ earth.html
+ magrethea.html

The sample output from ttree shown here indicates that two page templates, earth.html and magrethea.html, were found in the src directory. The + character to the left of the filenames indicates that the templates were processed successfully. Corresponding earth.html and magrethea.html files will have been created in the html directory con taining the output generated by processing the templates.

Now that we’ve set up ttree and told it where our page templates are located, we can add new pages to the site by simply adding them to the src directory. When you next run the build script, ttree will locate the new page templates, even if they’re located deep in a subdirectory (thanks to the recurse option), and process them into the corresponding place in the html directory.

You can now build all the static web pages in your site using a single, simple command.

Skipping Unmodified Templates

When ttree is run it tries to be smart in working out which templates need to be processed and which don’t. It does this by comparing the file modification time of the page template with the corresponding output file (if any) that it previously generated.

Run the bin/build script again, and the + characters to the left of the filename change to the - character:

$ bin/build
ttree 2.63 (Template Toolkit version 2.10)

      Source: /home/dent/web/src
 Destination: /home/dent/web/html
Include Path: [ /home/dent/web/lib ]
      Ignore: [ \b(CVS|RCS)\b, ^# ]
        Copy: [ \.(png|gif|jpg)$ ]
      Accept: [ * ]

-earth.html       (not modified)
-magrethea.html   (not modified)

This indicates that the templates weren’t processed the second time around, with the message to the right of the filenames explaining why. In this case, ttree has recog nized that the source templates, src/earth.html and src/magrethea.html, haven’t been modified since the corresponding output files, html/earth.html and html/magrethea. html, were created. Given that nothing has changed, there’s no need to reprocess the templates.

There may be times when you want to force ttree to build a particular page or even all the pages on the site, regardless of any file modification times. You can process one or more pages by naming them explicitly on the command line:

$ bin/build earth.html magrethea.html

One time that you might want to force all pages to be rebuilt is when you modify a header, footer, or some other template component that is used by all the pages. Unfortunately, ttree isn’t smart enough to figure out which library templates are used by which page templates. * The
-a option tells ttree to ignore file modification times and process all page templates, regardless:

$ bin/build -a

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: