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.

<use title>
Navigation bar
Main
Example

Copyright by Joe Doe.
last updated

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:

Here are the contents

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

Photo
Room:
Phone:
Fax:
Email:

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 page explains how to use sections. The htp file should define macros for each section. The htt file uses these macro in a while loop. This file defines several macros numbered from one to three. Here is the corresponding template tut4.htt.

On This Page

  • .

In lines 5-9 the table of contents is generated. The macro 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