How to use templates
Instead of having the header and footer in separate files as in the
previous section, it is often more convenient to have them in a single
file, which is a template from which the web pages are produced.
A simple example
Such a template would look as follows. Click here to download tut3.htt.
2. <head><title><use title></title></head>
6. Navigation bar<br>
7. <a href="main.html">Main</a><br>
8. <a href="example.html">Example</a><br>
11. <use body>
15. Copyright by <a href="firstname.lastname@example.org">Joe Doe</a>.<br>
16. last updated <file date>
The contents have to be stored into the macro body and are inserted in
line 11. To define a macro whose contents span multiple lines you can
use the block tag. Here is the
file tut3.htp that makes use of the template:
1. <file template="tut3.htt">
3. <set title="template example">
4. <block name="body">
5. <h2>Here are the contents</h2>
7. As you can see the file contains almost only the contents.
In line 1 you can see yet another syntax of the file tag. The template
attribute is similar to include attribute, but the
inclusion is deferred until the file is completely processed and the
macros are defined. You can have at most one template per file but
this is no serious limitation. Of course, the template and the main
file can include other files with the <file include> syntax.
You can see the strength of this template mechanism if you compare
this reference manual with the previous at http://math.newcastle.edu.au/~rking/htp/. As you can see the layout changed drastically, however,
this was achieved by changing only a single file, the template file.
Including more than one piece of information
Of course, you can have more than one place in your template where
macros are expanded. Here is an example that displays informations
about employees in a common layout. For each employee you have a htp
file, which just defines the personal informations in several macros,
and a single template file, that lays out the informations:
The template employees.htt looks like this:
1. <file template="employees.htt">
3. <set name="Jochen Hoenicke">
4. <set photo="photos/jochen.hoenicke.gif">
5. <set room="Bld.52 00-020">
6. <set phone="+49 761 203-8243">
7. <set fax="+49 761 203-8242">
1. <set title="$name">
2. <file include="header.hti">
4. <h1><use name></h1>
6. <if photo>
7. <img src=$photo alt="Photo">
11. <if room>
12. <tr><td><b>Room:</b></td><td><use room></td></tr>
14. <if phone>
15. <tr><td><b>Phone:</b></td><td><use phone></td></tr>
17. <if fax>
18. <tr><td><b>Fax:</b></td><td><use fax></td></tr>
20. <if email>
21. <tr><td><b>Email:</b></td><td><a href="mailto:$email"><use email></a></td></tr>
24. <file include="footer.hti">
This example shows, that you can mix includes and templates. In
this case the template includes a common header and footer, which may
be shared with several templates. In line 6 and several other you can
see the if tag. This tag can be
used to check if a variable is defined or if it has a certain value.
In this case the tag is used to omit information that wasn't defined
for this person.
How to include numbered sections
One feature of htp is a loop construct that enables you to create
numbered sections. Here is the example for the contents file.
This file defines several macros numbered from one to three. Here is
the corresponding template tut4.htt.
1. <file template="tut4.htt">
2. <set title="How To Use Sections">
4. <set title1="Introduction">
5. <block name=para1>
6. This page explains how to use sections.
9. <set title2="The htp file">
10. <block name=para2>
11. The htp file should define macros for each section.
14. <set title3="The htt file">
15. <block name=para3>
16. The htt file uses these macro in a while loop.
In lines 5-9 the table of contents is generated. The macro
1. <file include="header.hti">
3. <h1>On This Page</h1>
5. <set i=1>
6. <while para$i>
7. <li><use i>. <a href="#sec$i"><use title$i></a></li>
8. <inc i>
14. <set i=1>
15. <while para$i>
16. <h2><a name="#sec$i"><use title$i></a></h2>
17. <use para$i>
18. <inc i>
21. <file include="footer.hti">
i is used as counter variable. In line 5 it is
initialized to one. The while loop in lines 6-9 is repeatly executed
until the macro
para$i is no longer defined. It
creates a list item, prints out the section number, then a link to
#sec$i on the current page, and writes out the title of
the section. In line 8 the macro
i is incremented. An
analogous mechanism is used to include the sections in lines 14-19.
This closes our introduction to templates. In the next section you will find some hints how you
can organize your htp projects