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