public final class TemplateModelUtils
extends java.lang.Object
TemplateModel
-s that didn't fit elsewhere.Modifier and Type | Method and Description |
---|---|
static TemplateHashModelEx2.KeyValuePairIterator |
getKeyValuePairIterator(TemplateHashModelEx hash)
TemplateHashModelExKeyValuePairIterator that even works for a non-TemplateHashModelEx2
TemplateHashModelEx . |
static TemplateHashModel |
wrapAsHashUnion(ObjectWrapper objectWrapper,
java.util.List<?> hashLikeObjects)
Creates a
TemplateHashModel that is the union of the hash-like objects passed in as argument. |
static TemplateHashModel |
wrapAsHashUnion(ObjectWrapper objectWrapper,
java.lang.Object... hashLikeObjects)
Same as
wrapAsHashUnion(ObjectWrapper, List) , but uses a varargs parameter instead of a List . |
public static final TemplateHashModelEx2.KeyValuePairIterator getKeyValuePairIterator(TemplateHashModelEx hash) throws TemplateModelException
TemplateHashModelExKeyValuePairIterator
that even works for a non-TemplateHashModelEx2
TemplateHashModelEx
. This is used to simplify code that needs to iterate through the key-value pairs of
TemplateHashModelEx
-s, as with this you don't have to handle non-TemplateHashModelEx2
-s
separately. For non-TemplateHashModelEx2
values the iteration will throw TemplateModelException
if it reaches a key that's not a string (TemplateScalarModel
).TemplateModelException
public static TemplateHashModel wrapAsHashUnion(ObjectWrapper objectWrapper, java.lang.Object... hashLikeObjects) throws TemplateModelException
wrapAsHashUnion(ObjectWrapper, List)
, but uses a varargs parameter instead of a List
.TemplateModelException
public static TemplateHashModel wrapAsHashUnion(ObjectWrapper objectWrapper, java.util.List<?> hashLikeObjects) throws TemplateModelException
TemplateHashModel
that is the union of the hash-like objects passed in as argument. Hash-like
here means that the argument ObjectWrapper
will wrap it into an TemplateModel
that implements
TemplateHashModel
, or it's already a TemplateHashModel
. (Typical hash-like objects are JavaBeans
and Map
-s, though it depends on the ObjectWrapper
.)
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 with Configuration.setSharedVariables(Map)
; check if that fits your use case better.
objectWrapper
- ObjectWrapper
used to wrap the elements of hashLikeObjects
, except those that are
already TemplateModel
-s. Usually, you should pass in Configurable.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 be null
, but the list can contain
null
elements, which will be silently ignored. The list can be empty, in which case the result
is an empty hash.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 in
hashLikeObjects
are TemplateHashModelEx
objects (or if there are 0 elements), then the
result will implement TemplateHashModelEx
as well.TemplateModelException
- If wrapping an element of hashLikeObjects
fails with TemplateModelException
, or if
wrapping an element results in a TemplateModel
that's not a TemplateHashModel
, or if
the element was already a TemplateModel
that isn't a TemplateHashModel
.