|
Hash ModelHash models are probably the most common model you'll use in FreeMarker.
Like public TemplateModel get(java.lang.String key) throws TemplateModelException; FreeMarker uses this method to return a value for a given key name. This
method is similar to the If the specified key does not exist in the hash, the results are
implementation dependant. Some people may throw a
Similarly, if the Hash models are powerful because humans tend to recognise things in terms of names rather than numbers. Hash models can return other models. A hash model can contain other hash models, and so on (as we saw in the introduction). Key NamesKey names should not include any dots (".") as part of the name. This is
because FreeMarker uses the dot character to determine when a key name
is complete, and to then traverse into the next TemplateModel. If you're
tempted to use a dot in your key name, try a different separator instead, or
make sure you always use the Dynamic Key Name operator (the Finally, key names beginning with an underscore ("_") should be avoided. These may be used as special keys by future versions of FreeMarker. Getting started quicklyTo quickly roll your own hash model, you can use either
the Extended Functionality for HashesThe 2.0rc2 release introduced a subinterface of TemplateHashModel, called TemplateHashModelEx. This interface defines 3 extra methods: size(), keys() and values(). If a model implements this interface, certain built-ins are available:
Note that the default implementation, SimpleHash already implements the TemplateHashModelEx interface, and thus, the above keys will work. Note that this is what is behind the removal of the class freemarker.template.utility.ExtendedHash, since its existence is now superfluous, as SimpleHash contains all of the additional functionality that ExtendedHash used to contain. |
|