TemplateModelUtils (FreeMarker 2.3.35 API)
Description: declaration: package: freemarker.template.utility, class: TemplateModelUtils

Package freemarker.template.utility

# Class TemplateModelUtils

java.lang.Object
freemarker.template.utility.TemplateModelUtils

public final class TemplateModelUtils extends Object
Static utility method related to `TemplateModel`-s that didn't fit elsewhere.

Since:
  2.3.28

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static TemplateHashModelEx2.KeyValuePairIterator`
  `getKeyValuePairIterator​(TemplateHashModelEx hash)`
  `TemplateModelUtils.TemplateHashModelExKeyValuePairIterator` that even works for a non-`TemplateHashModelEx2` `TemplateHashModelEx`.
  `static TemplateHashModel`
  `wrapAsHashUnion​(ObjectWrapper objectWrapper, Object... hashLikeObjects)`
  Same as `wrapAsHashUnion(ObjectWrapper, List)`, but uses a varargs parameter instead of a `List`.
  `static TemplateHashModel`
  `wrapAsHashUnion​(ObjectWrapper objectWrapper, List<?> hashLikeObjects)`
  Creates a `TemplateHashModel` that is the union of the hash-like objects passed in as argument.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### getKeyValuePairIterator

    public static final TemplateHashModelEx2.KeyValuePairIterator getKeyValuePairIterator​(TemplateHashModelEx hash) throws TemplateModelException
    `TemplateModelUtils.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`).
    Throws:
      `TemplateModelException`
    Since:
      2.3.28

  - ### wrapAsHashUnion

    public static TemplateHashModel wrapAsHashUnion​(ObjectWrapper objectWrapper, Object... hashLikeObjects) throws TemplateModelException
    Same as `wrapAsHashUnion(ObjectWrapper, List)`, but uses a varargs parameter instead of a `List`.
    Throws:
      `TemplateModelException`
    Since:
      2.3.29

  - ### wrapAsHashUnion

    public static TemplateHashModel wrapAsHashUnion​(ObjectWrapper objectWrapper, List<?> hashLikeObjects) throws TemplateModelException
    Creates a `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.

    Parameters:
      `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.
    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 in `hashLikeObjects` are `TemplateHashModelEx` objects (or if there are 0 elements), then the result will implement `TemplateHashModelEx` as well.
    Throws:
      `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`.
    Since:
      2.3.29