Perl Programming Page 4 - Building a Complete Website using the Template Toolkit |
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' -%] 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'; instead of the more verbose: [% title = 'Earth' -%] 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 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 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 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 -%] <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).
|
|
|
|
|
|
|
|