#18 Enhance: Support for multiple models

closed-fixed
Nicholas Cull
None
5
2001-05-06
2001-04-17
Nicholas Cull
No

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:

template.keyname

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.

Discussion

  • Nicholas Cull
    Nicholas Cull
    2001-04-17

    Allows classes implementing multiple models to work as expected

     
    Attachments
  • Nicholas Cull
    Nicholas Cull
    2001-05-06

    Logged In: YES
    user_id=137221

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

     
  • Nicholas Cull
    Nicholas Cull
    2001-05-06

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