Package freemarker.template.utility
Class TemplateModelUtils
java.lang.Object
freemarker.template.utility.TemplateModelUtils
Static utility method related to
TemplateModel
-s that didn't fit elsewhere.- Since:
- 2.3.28
-
Method Summary
Modifier and TypeMethodDescriptionTemplateModelUtils.TemplateHashModelExKeyValuePairIterator
that even works for a non-TemplateHashModelEx2
TemplateHashModelEx
.static TemplateHashModel
wrapAsHashUnion(ObjectWrapper objectWrapper, Object... hashLikeObjects)
Same aswrapAsHashUnion(ObjectWrapper, List)
, but uses a varargs parameter instead of aList
.static TemplateHashModel
wrapAsHashUnion(ObjectWrapper objectWrapper, List<?> hashLikeObjects)
Creates aTemplateHashModel
that is the union of the hash-like objects passed in as argument.
-
Method Details
-
getKeyValuePairIterator
public static final TemplateHashModelEx2.KeyValuePairIterator getKeyValuePairIterator(TemplateHashModelEx hash) throws TemplateModelExceptionTemplateModelUtils.TemplateHashModelExKeyValuePairIterator
that even works for a non-TemplateHashModelEx2
TemplateHashModelEx
. This is used to simplify code that needs to iterate through the key-value pairs ofTemplateHashModelEx
-s, as with this you don't have to handle non-TemplateHashModelEx2
-s separately. For non-TemplateHashModelEx2
values the iteration will throwTemplateModelException
if it reaches a key that's not a string (TemplateScalarModel
).- Throws:
TemplateModelException
- Since:
- 2.3.28
-
wrapAsHashUnion
public static TemplateHashModel wrapAsHashUnion(ObjectWrapper objectWrapper, Object... hashLikeObjects) throws TemplateModelExceptionSame aswrapAsHashUnion(ObjectWrapper, List)
, but uses a varargs parameter instead of aList
.- Throws:
TemplateModelException
- Since:
- 2.3.29
-
wrapAsHashUnion
public static TemplateHashModel wrapAsHashUnion(ObjectWrapper objectWrapper, List<?> hashLikeObjects) throws TemplateModelExceptionCreates aTemplateHashModel
that is the union of the hash-like objects passed in as argument. Hash-like here means that the argumentObjectWrapper
will wrap it into anTemplateModel
that implementsTemplateHashModel
, or it's already aTemplateHashModel
. (Typical hash-like objects are JavaBeans andMap
-s, though it depends on theObjectWrapper
.)This method is typical used when you want to compose a data-model from multiple objects in a way so that their entries (
Map
key-value pairs, bean properties, etc.) appear together on the top level of the data-model. In such case, use the return value of this method as the combined data-model. Note that this functionality somewhat overlaps withConfiguration.setSharedVariables(Map)
; check if that fits your use case better.- Parameters:
objectWrapper
-ObjectWrapper
used to wrap the elements ofhashLikeObjects
, except those that are alreadyTemplateModel
-s. Usually, you should pass inConfigurable.getObjectWrapper()
here.hashLikeObjects
- Hash-like objects whose union the result hash will be. The content of these hash-like objects must not change, or else the behavior of the resulting hash can be erratic. If multiple hash-like object contains the same key, then the value from the last such hash-like object wins. The oder of keys is kept, with the keys of earlier hash-like object object coming first (even if their values were replaced by a later hash-like object). This argument can't benull
, but the list can containnull
elements, which will be silently ignored. The list can be empty, in which case the result is an empty hash.- Returns:
- The
TemplateHashModel
that's the union of the objects provided. This is a "view", that delegates to the underlying hashes, not a copy. The object is not thread safe. If all elements inhashLikeObjects
areTemplateHashModelEx
objects (or if there are 0 elements), then the result will implementTemplateHashModelEx
as well. - Throws:
TemplateModelException
- If wrapping an element ofhashLikeObjects
fails withTemplateModelException
, or if wrapping an element results in aTemplateModel
that's not aTemplateHashModel
, or if the element was already aTemplateModel
that isn't aTemplateHashModel
.- Since:
- 2.3.29
-