HTP

Source code: ref/template.htp

Table of Contents
  htp.def
  macros.def
  htp.htt

<BLOCK body>

<H2>How to use templates</H2>

<p>
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.  
</p>

<H3>A simple example</H3>

<p>
Such a template would look as follows.  Click here to download <a href="tut/tut3.htt">tut3.htt</a>.
</p>

<htpcode file="tut/tut3.htt" linenr>
<html>
 <head><title><use title></title></head>
 <body>
  <table>
   <tr><td>
     Navigation bar<br>
     <a href="main.html">Main</a><br>
     <a href="example.html">Example</a><br>
   </td>
   <td>
     <use body>
   </td></tr>
  </table>
  <hr>
  Copyright by <a href="webmaster@domain.com">Joe Doe</a>.<br>
  last updated <file date>
 </body>
</html>
</htpcode>

<p>
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 <manlink href="block.html">block</manlink> tag.  Here is the
file <a href="tut/tut3.htp">tut3.htp</a> that makes use of the template:
</p>

<htpcode file="tut/tut3.htp" linenr>
<file template="tut3.htt">

<set title="template example">
<block name="body">
 <h2>Here are the contents</h2>
 <p>
 As you can see the file contains almost only the contents.
 </p>
</block>
</htpcode>

<p>
In line 1 you can see yet another syntax of the <manlink
href="file.html">
file</manlink> tag.  The <emph>template</emph>
attribute is similar to <emph>include</emph> 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 &lt;<manlink
href="file.html">
file</manlink> include&gt; syntax.
</p>

<P>You can see the strength of this template mechanism if you compare
this reference manual with the previous at <A HREF="${rk_htp}"><use
rk_htp>
</A>.  As you can see the layout changed drastically, however,
this was achieved by changing only a single file, the template file.</P>

<H3>Including more than one piece of information</H3>

<p> 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: </p>

<htpcode file="tut/jh.htp" linenr>
<file template="employees.htt">

<set name="Jochen Hoenicke">
<set photo="photos/jochen.hoenicke.gif">
<set room="Bld.52 00-020">
<set phone="+49 761 203-8243">
<set fax="+49 761 203-8242">
</htpcode>

The template <a href="tut/employees.htt">employees.htt</a> looks like this:

<htpcode file="tut/employees.htt" linenr>
<set title="$name">
<file include="header.hti">

<h1><use name></h1>

<if photo>
<img src=$photo alt="Photo">
</if>

<table>
<if room>
<tr><td><b>Room:</b></td><td><use room></td></tr>
</if>
<if phone>
<tr><td><b>Phone:</b></td><td><use phone></td></tr>
</if>
<if fax>
<tr><td><b>Fax:</b></td><td><use fax></td></tr>
</if>
<if email>
<tr><td><b>Email:</b></td><td><a href="mailto:$email"><use email></a></td></tr>
</if>
</table>
<file include="footer.hti">
</htpcode>

<p>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 <manlink href="if.html">if</manlink> 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.</p>

<H3>How to include numbered sections</H3>

One feature of htp is a loop construct that enables you to create
numbered sections.  Here is the example for the contents file.  

<htpcode file="tut/tut4.htp" linenr>
<file template="tut4.htt">
<set title="How To Use Sections">

<set title1="Introduction">
<block name=para1>
This page explains how to use sections.
</block>

<set title2="The htp file">
<block name=para2>
The htp file should define macros for each section.
</block>

<set title3="The htt file">
<block name=para3>
The htt file uses these macro in a while loop.
</block>
</htpcode>

This file defines several macros numbered from one to three.  Here is
the corresponding template <a href="tut/tut4.htt">tut4.htt</a>.

<htpcode file="tut/tut4.htt" linenr>
<file include="header.hti">

<h1>On This Page</h1>
<ul>
<set i=1>
<while para$i>
 <li><use i>. <a href="#sec$i"><use title$i></a></li>
 <inc i>
</while>
</ul>

<hr>

<set i=1>
<while para$i>
 <h2><a name="#sec$i"><use title$i></a></h2>
 <use para$i>
 <inc i>
</while>

<file include="footer.hti">
</htpcode>

In lines 5-9 the table of contents is generated.  The macro
<code>i</code> 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 <code>para$i</code> is no longer defined.  It
creates a list item, prints out the section number, then a link to
<code>#sec$i</code> on the current page, and writes out the title of
the section.  In line 8 the macro <code>i</code> is incremented.  An
analogous mechanism is used to include the sections in lines 14-19.


<p>This closes our introduction to templates.  In the <a
href="default.html">
next section</a> you will find some hints how you
can organize your htp projects</p>

</block>

  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 Tue Feb 22, 2011