Data Models

List Model

List models are like Arrays or LinkedLists in Java. A TemplateListModel2 gives you an ordered list of elements, which can be iterated over, using foreach or list syntax.

To support these operations, the TemplateListModel2 interface is more complex, as we see below. The following methods allow FM-Classic to iterate over a list:

    public TemplateIteratorModel templateIterator() throws TemplateModelException;

    public void releaseIterator(TemplateIteratorModel iterator);

The templateIterator() method returns an iterator, similar to java.util.Iterator, that can be used to iterate over a list. If there are no elements in the list, this method can safely return null, or the isEmpty() method can return true.

The releaseIterator() method is called once the iterator is no longer required. This can be useful if the list model performs pooling of iterators. Otherwise, the implementation can be left blank.

List Iterators

The TemplateIteratorModel looks like the following:

    public boolean hasNext() throws TemplateModelException;

    public TemplateModel next() throws TemplateModelException;

The hasNext() and next() methods are used to determine when we're at the end of a list, and to return the next element in the list respectively. These correspond to the same method names in the java.util.Iterator interface.

The isEmpty() method determines whether there are any elements in the iterator model. If there aren't, the iterator is released immediately.

Writeable Iterator

The TemplateWriteableIteratorModel extends TemplateIteratorModel to provide a writeable list model. A TemplateListModel2 can choose to return either model from its templateIterator method.

The TemplateWriteableIteratorModel adds the following method:

    public void set(TemplateModel model) throws TemplateModelException;

The set() method overwrites the last value returned by the iterator's next() method.

The TemplateListModel interface provides backward compatibility for earlier versions of FreeMarker. It should not be used for new models.