The TemplateModelRoot Interface

The TemplateModelRoot interface is the same as the TemplateHashModel interface. Indeed, TemplateModelRoot is a sub-interface of TemplateHashModel. 4 two additional method signatures are given below:

public void put(java.lang.String key,
                TemplateModel model);

public void remove(java.lang.String key);

public Locale getLocale();

public void setLocale(Locale l);
        

TemplateModelRoot is required at the root of any FreeMarker TemplateModel structure.

Why?

Because of the <assign>, <list> and <foreach> operators. For these operators to work, the root of the TemplateModel structure must be writable as well as readable. TemplateRootModel provides this by allowing new keys to be added (the put() method), as well as deleted (the remove() method). These operations correspond to variables falling in and out of scope.

Note also that we only need the root of the model to be writable. None of the above operators can assign values below the root of a TemplateModel.

Note that the last 2 methods above were added in FreeMarker 2, and are really just hooks for later use.

Getting started quickly

To quickly create a TemplateModelRoot, you can either use a freemarker.template.SimpleHash, or you can wrap an arbitrary implementation of java.util.Map into a freemarker.ext.beans.RootMapModel. In this later case, map elements can be arbitrary objects, and need not be TemplateModel instances as they will be subject to automatic JavaBeans wrapping.