Each node in the data model tree must implement one of the subinterfaces of TemplateModel. The root node must implement TemplateModelRoot. The other nodes must implement TemplateHashModel, TemplateListModel, TemplateScalarModel, or TemplateMethodModel. See the javadoc documentation for detailed descriptions of these interfaces.
Simple implemations of hashes, lists, and scalars have been provided: SimpleScalar, SimpleList and SimpleHash. Unless you're doing something highly unusual, you'll want to use a SimpleHash for the root node. In many cases, it will be sufficient to copy your data from its original source into a tree of SimpleScalar, SimpleList and SimpleHash objects. If you want to implement lazy evaluation, or if you want to wrap existing data objects for use with templates, you'll want to make adapter or wrapper classes that implement the TemplateModel interfaces. See the sample code for an example.
SimpleList has a number of convenient features. If a TemplateListModel needs to be cached, a SimpleList can make a copy of it. If you're using a SimpleList as a cache, you can also give it a shelf life; its owner (e.g. a servlet) can then ask it, before each request is processed, whether it's gotten stale, and refresh it if necessary.
Previous: Compressing Whitespace | Next: Sample Code |