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.TemplateHashModelExKeyValuePairIteratorthat even works for a non-TemplateHashModelEx2TemplateHashModelEx.static TemplateHashModelwrapAsHashUnion(ObjectWrapper objectWrapper, Object... hashLikeObjects)Same aswrapAsHashUnion(ObjectWrapper, List), but uses a varargs parameter instead of aList.static TemplateHashModelwrapAsHashUnion(ObjectWrapper objectWrapper, List<?> hashLikeObjects)Creates aTemplateHashModelthat 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.TemplateHashModelExKeyValuePairIteratorthat even works for a non-TemplateHashModelEx2TemplateHashModelEx. 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-TemplateHashModelEx2values the iteration will throwTemplateModelExceptionif 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 aTemplateHashModelthat is the union of the hash-like objects passed in as argument. Hash-like here means that the argumentObjectWrapperwill wrap it into anTemplateModelthat 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 (
Mapkey-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-ObjectWrapperused 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 containnullelements, which will be silently ignored. The list can be empty, in which case the result is an empty hash.- Returns:
- The
TemplateHashModelthat'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 inhashLikeObjectsareTemplateHashModelExobjects (or if there are 0 elements), then the result will implementTemplateHashModelExas well. - Throws:
TemplateModelException- If wrapping an element ofhashLikeObjectsfails withTemplateModelException, or if wrapping an element results in aTemplateModelthat's not aTemplateHashModel, or if the element was already aTemplateModelthat isn't aTemplateHashModel.- Since:
- 2.3.29
-