Version 0.7
4/19/1999
java package com.destinysoftware.templates
new since the last version / new last time / old stuff
|
Overview |
This is an HTML template system for dynamic HTML content generation in java. It separates the HTML from java objects by providing a flexible pseudo-tag system for referencing java objects from the HTML template. It is not a java library for the generation of HTML. Even though this is the web, it still makes sense to build an application around an object model that describes a domain, and it is still desireable to separate this application from a particular user interface technology, particularly HTML. This system provides part of the foundation of a system that lets the front end to adapt to the application rather than the other way around. |
An HTML template is a portion of HTML that contains special tags for the template system. Templates can be anything from one entire page to a single word. Templates are compiled at "startup" time and stored as hierarchical collections of java objects which model the behavior of the various pseudo-tags that comprise the template vocabulary. This allows efficient HTML construction as needed.
An HTML template contains special tags that reference the fields of a java object
by name. The tags are of two kinds: atomic tags, such as <Field
varname> which will substitute the tag with the result of varname.toString()
in java; and collections, such as <List
varname> .... </List> which expects varname to support the java method
Enumeration elements() and process the stuff between the List
tags for each element of the collection varname.
This dynamic lookup and execution is done via the java.lang.reflect package which allows the java object to remain independent of the templatization system - there are no object model or interface constraints.
The out of date example "templateTest" demonstrates the basic features using the HTML template ttest.html. To run it, set your classpath to this directory (template system) and run the demo with "java templateTest". It is illustrated in some detail in example.html.
| All, Count, DElementField, DField, ElementField, EncodeHTML, Field, Include, IncludeIf, Insert, InvokeMethod, List, Name, RInput, UseContext, Value, XL | Templatization is done within a context, that is, tags which reference objects find
them within the current context. A context may either be a general java object or
a java.util.Dictionary subclass (such as Hashtable). Phrases such as "gets the
named object" generally also implies that and the results of its toString()
method is inserted into the output. Tag names are case-insensitive; the italics R_
prefix on tag names indicates it is optional (both names with and without are in
the default tag name dictionary).The tags <RInput> and <XL> are different from
the rest in that a) they produce HTML tags as output and b) except for the few attributes
they know, attributes are "passed through" to the output tag. They provide
an attribute tag which specifies the HTML tag name to use. |
<R_Insert if="var" then="template1"
else="template2" context="c">
<R_Insert template="template1">
<R_Insert template1><R_Include object="obj" context="c">
section </Include>
<R_IncludeIf obj> section </Include>
<R_Field name="f">
<R_Field name="f1.f2">
<R_Field f>
<R_Field f1.f2><R_DField name="f">
<R_DField f>elements(), such as Vector or Hashtable; section
is processed for each element of the collection. Use <XL> when possible instead
of <R_List>
<R_List object="thelist" count="true">
section </List>
<R_List thelist> section </List><List>
and <XL>.
<R_ElementField name="f">
<R_ElementField name="f1.f2">
<R_ElementField f1.f2>
<R_Name>
<R_Name> will give
the key of the current element. This works within both <List>
and <XL>.
<Key>
java.util.Dictionary. Valid
only in <List> and <XL>.
<Count>
<R_Value>
<RInput tag="tagname" name="f1"
attributes>
<tagname name="f1" value="<Field
f1>" attributes>
<RInput tag="tagname" name="f1.f2"
attributes>
<tagname name="f1.f2" value="<Field
f1.f2>" attributes>
<List> but can replace HTML tags such as <UL>,
<TR>, etc which are to be repeated
<XL tag="tagname" object="theList" count="true"
attributes > content </XL>
<tagname attributes> content
</tagname> <
> & " to the HTML sequences < > &
". Can be used to make sure that text has no HTML structural sequences
or easily produce examples, etc. by showing the real HTML and showing the source
as well.
<EncodeHTML name="f"> section
</EncodeHTML>
<All encoding="e" name="f">
%xx where x is a hex digit of the char in ASCII.
< > & " values
to the HTML sequences < > & "<UseContext name="f"> section </UseContext>toString() of the result. Atomic.
<InvokeMethod name="method">
<InvokeMethod method>
com.destinysoftware.templates.input and com.destinysoftware.templates.compositeList.
Added to the templateParser default vocabulary as "RInput" and "XL"
respectively. Input is a subclass of ObjectField (<Field>) and acts accordingly.com.destinysoftware.templates.field
(superclass of ObjectField and DictionaryField) modified to invoke setAttribute()
so it can be extended, as imput and compositeList required. But since it parses for
the syntax "a.b" meaning field b of field a, when this is found, it presumes
that when there is a b component, that field.subFieldName should be
set to it. setAttribute(String, String) possibly should be modified
to be setAttribute(String, String[])
all creates a "big string" -- templateParser.loadDefaultClasses()
adds this as "All".
htmlEncode -- templateParser.loadDefaultClasses()
adds this as "EncodeHTML". implements the new abstract class transform
for doing such things, even postprocessing the output of other tags.
<R_DField x>, the equivalent of <R_Field>
(and <R_TEXT>) for getting x from within a java.util.Dictionary.
This is the templateObject class DictionaryField
<R_DElementField x> ; in a List of Dictinary objects,
this gets x from the Dictionary. for example
<R_List theList> <R_DElementField a> </R_List>could be used for this code:
Vector theList;
Hashtable h1, h2;
h1.add("a", "hey!");
h2.add("a", "yow!");theList.add(h1);
theList.add(h2);would simply display "hey! yow!".
ElementField (tag <R_CALL varname>) is
implemented. This tag is valid only inside an R_LIST or R_CLIST which returns the
toString() of a field of an element object in the collection. It's called
R_CALL for backwards reasons, and I will also provide an additional and more appropriate
tag name like I've done with <R_SessionID> (better than <APPID>).
Russell Holt
March, April 1999