Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo


#18 Enhance: Support for multiple models

Nicholas Cull
Nicholas Cull

This patch allows template classes that implement more than one TemplateModel to work as expected.

In particular, classes that implement both TemplateHashModel and TemplateListModel now enjoy a more predictable behaviour. The behaviour is as follows:

When using dynamic key names, for instance:

template[ 4 ]
template[ "4" ]
template[ keyname ]
template[ "keyname" ]

FreeMarker will first check whether the class implements a TemplateListModel. If so, it will then attempt to parse the key as an Integer. If it succeeds, the integer will be used as an index into the TemplateListModel.

If the key name cannot be resolved as an integer, FreeMarker next checks if the key name is the string "size". If so, it will get the TemplateModel from the listSize() method of the TemplateListModel.

If the key name is still unresolved, and the class also implements TemplateHashModel, FreeMarker will use the key name as a lookup into the TemplateHashModel.

Similarly, when using dot notation, such as:


FreeMarker first determines whether the class implements a TemplateHashModel. If so, it will lookup the key name using the TemplateHashModel.

If the result was null, _and_ the key name was "size", _and_ the class also implements TemplateListModel, FreeMarker will then call the listSize() method of the TemplateListModel.

Otherwise, if the class does not implement TemplateHashModel, and does implement TemplateListModel, and the key name was "size", FreeMarker returns the listSize() method.


All of this is a very long-winded way of saying the FreeMarker now does what you expect in these cases. Both dynamic key names and dot notation will work -- dynamic key names favour the ListModel, while dot notation favours HashModel.


  • Nicholas Cull
    Nicholas Cull

    Allows classes implementing multiple models to work as expected

  • Nicholas Cull
    Nicholas Cull

    Logged In: YES

    Checked in in a modified form: removed the coding for "size" key, since listSize() is probably going to disappear soon.

  • Nicholas Cull
    Nicholas Cull

    • assigned_to: nobody --> run2000
    • status: open --> closed-fixed