Template Documentation

The following is an extract from the documentation for the templating process. It describes the template constructs available for use by the template writers.


The TemplateNamed construct is used to refer to a named template. This class is used to retrieve the template by name when necessary. It may also be called directly when necessary. It treats the template-name as a file-name in the template-directory, unless a full file-name is given, beginning with a leading slash.

Currently, this class regards the name as referring to a file in which the template resides. This file is parsed by TemplateParser, but because that class is not public, here is the documentation on the format of the file:

The format of the file is arbitrary text, interspersed with template tags of the following format:

A slot to be replaced with data from the dynamic microdocuments:

    <?template slot 3 order.items>
                                                                  
A slot as above, but with tags renamed:
    <?template slot 3 items oldName1 newName1 oldName2 newName2>
                                                                  
As above, but with no tags at all:
    <?template slot 3 items[0].price #BARE>
                                                                  
As above, but with strings automatically quoted:
    <?template slot 3 items[0].price #QUOTE>
                                                                  
Note that this is intended to provide an attribute value when the template writer isn't sure what type of quote won't appear within the field. #QUOTE will generate surrounding "s unless the content contains a ", in which case, if the content doesn't contain a ', it will use 's, else it will generate "s but with inner "s translated into &quot;

As above, but with strings url-encoded:

    <?template slot 3 items[0].price #URL>
                                                                  
Note that URL-encoding should actually be done in the stylesheets. It was added to the templating language as an expediency when stylesheets were not being used. Rujith 1998-08-27

Obtain the number of elements in a sequence:

    Your search returned <?template slot 1 results.#> result(s).
                                                                  
A looping construct:
    <?template loop 0 results>
      arbitrary contents for each item in the result, in which the
      -1 refers to the current item being processed, and @ returns
      the index of the current item being processed:
      <?template slot -1 @> <?template slot -1 title>
    <?template /loop>
                                                                  

The loop actually supports lots of options:

    <?template loop 1 pages>
      ... stuff at beginning of loop ...
      <?template item>
        ... stuff for each iteration of the loop ...
      <?template separator>
        ... stuff to insert between each iteration ...
      <?template end>
        ... stuff after all the iterations ...
      <?template none>
        ... stuff if loop had no iterations
    <?template /loop>
                                                                  

Use the 'item' construct only if you want to have stuff at the beginning of the loop. Just omit it otherwise, then the stuff is just assumed to be the per-item output.

A named template inclusion, optionally relative to the enclosing template:

    <?template name resultSubTemplate>
    <?template name subTemplateName #RELATIVE>
    <?template name ../../foo/bar/templName #RELATIVE>
                                                                  
A conditional construct:
    <?template if 0 anyFound>
      arbitrary contents if specified boolean field is true, or if
      string-field is non-empty, or if integer or long field is
      non-zero 
    <?template else>
      arbitrary contents otherwise
    <?template /if>
                                                                  
The <else> tag in the above, and the corresponding contents, may be omitted. In addition, a string-equality or numeric equality test may be specified as follows.
     <?template if -1 fieldName fixedString>
     <?template if 1 fieldName fixedNumber>
     <?template if 2 fieldName 4 otherFieldName>
                                                                  

This permits a specified string, integer or long field to be tested against a fixed string or a fixed number, or against another string, integer or long field, respectively.

In SGML terms, these constructs appear as `processing instructions.' As such, they should pass unmolested and uninterpreted through SGML tools. However, note that these constructs may appear within legitimate tags, as in:

    <orderItem product="<?template slot -1 id #BARE>"
    quantity="<?template slot -1 quantity #BARE>">
                                                                  
In this case, they MUST be within quotes, and the corresponding attribute must be declared in the DTD to permit PCDATA values, for the template to be SGML-compliant. In general, the DTD for a doc-instance will be slightly different from the DTD for the corresponding template.
Rujith de Silva
Created 2001-02-07; modified 2002-10-26