HTP

htp on-line reference : Templates

Table of Contents
  Introduction
  License
  Tutorial
    Macros
    Including
  > Templates
    Files
    Metatags
  Usage
  HTP Tags
  History
  Wish list
  Bugs

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.

 1. <html>
 2.  <head><title><[ref]use title></title></head>
 3.  <body>
 4.   <table>
 5.    <tr><td>
 6.      Navigation bar<br>
 7.      <a href="main.html">Main</a><br>
 8.      <a href="example.html">Example</a><br>
 9.    </td>
10.    <td>
11.      <[ref]use body>
12.    </td></tr>
13.   </table>
14.   <hr>
15.   Copyright by <a href="webmaster@domain.com">Joe Doe</a>.<br>
16.   last updated <[ref]file date>
17.  </body>
18. </html>

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 [ref]block tag. Here is the file tut3.htp that makes use of the template:

 1. <[ref]file template="tut3.htt">
 2.
 3. <[ref]set title="template example">
 4. <[ref]block name="body">
 5.  <h2>Here are the contents</h2>
 6.  <p>
 7.  As you can see the file contains almost only the contents.
 8.  </p>
 9. <[ref]/block>

In line 1 you can see yet another syntax of the [ref]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 <[ref]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:

 1. <[ref]file template="employees.htt">
 2.
 3. <[ref]set name="Jochen Hoenicke">
 4. <[ref]set photo="photos/jochen.hoenicke.gif">
 5. <[ref]set room="A3 2-218">
 6. <[ref]set phone="+49 441 798-3124">
 7. <[ref]set fax="+49 441 798-2965">

The template employees.htt looks like this:

 1. <[ref]set title="$name">
 2. <[ref]file include="header.hti">
 3.
 4. <h1><[ref]use name></h1>
 5.
 6. <[ref]if photo>
 7. <[ref]img src=$photo alt="Photo">
 8. <[ref]/if>
 9.
10. <table>
11. <[ref]if room>
12. <tr><td><b>Room:</b></td><td><[ref]use room></td></tr>
13. <[ref]/if>
14. <[ref]if phone>
15. <tr><td><b>Phone:</b></td><td><[ref]use phone></td></tr>
16. <[ref]/if>
17. <[ref]if fax>
18. <tr><td><b>Fax:</b></td><td><[ref]use fax></td></tr>
19. <[ref]/if>
20. <[ref]if email>
21. <tr><td><b>Email:</b></td><td><a href="mailto:$email"><[ref]use email></a></td></tr>
22. <[ref]/if>
23. </table>
24. <[ref]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 [ref]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.

 1. <[ref]file template="tut4.htt">
 2. <[ref]set title="How To Use Sections">
 3.
 4. <[ref]set title1="Introduction">
 5. <[ref]block name=para1>
 6. This page explains how to use sections.
 7. <[ref]/block>
 8.
 9. <[ref]set title2="The htp file">
10. <[ref]block name=para2>
11. The htp file should define macros for each section.
12. <[ref]/block>
13.
14. <[ref]set title3="The htt file">
15. <[ref]block name=para3>
16. The htt file uses these macro in a while loop.
17. <[ref]/block>

This file defines several macros numbered from one to three. Here is the corresponding template tut4.htt.

 1. <[ref]file include="header.hti">
 2.
 3. <h1>On This Page</h1>
 4. <ul>
 5. <[ref]set i=1>
 6. <[ref]while para$i>
 7.  <li><[ref]use i>. <a href="#sec$i"><[ref]use title$i></a></li>
 8.  <[ref]inc i>
 9. <[ref]/while>
10. </ul>
11.
12. <hr>
13.
14. <[ref]set i=1>
15. <[ref]while para$i>
16.  <h2><a name="#sec$i"><[ref]use title$i></a></h2>
17.  <[ref]use para$i>
18.  <[ref]inc i>
19. <[ref]/while>
20.
21. <[ref]file include="footer.hti">

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

  page source
  htp project
  htp homepage
hosted by
SourceForge.net
HTML coding Powered by htp
htp on-line reference / http://htp.sourceforge.net/ref/
Authors: Jim Nelson, Jochen Hoenicke, Michael Möller.
Maintainers: Jochen Hoenicke.

Copyright © 1995-96 Jim Nelson.
Copyright © 2001-2003 Jochen Hoenicke.
Permission to reproduce and distribute this hypertext document granted according to terms described in the License section.

last updated Fri Aug 31, 2007