public interface RenderTransformer
A RenderTransformer handles the conversion of objects to be returned by
Template.renderToObject() etc.
Implementations may optimize how objects are actually appended/serialized, and when exceptions are thrown in the case that the final result would be too long.
The default implementation,RenderTransformerDefaultImpl, simply appends strings to a
StringBuilder. While this will work in many cases, there is a chance that large strings are
created temporarily on the heap, increasing the chance of an out-of-memory situation, and a generally
high allocation activity (spikes in the memory allocation graph).
For example, the following liquid template results in 10012 StringBuilder
#toString() conversions:
{% for l in (1..10)%}{% for k in (1..1000) %}
{% for i in (1..1000) %}Hello! {{ i }} world
{% endfor %}{% endfor %}{% endfor %}
With a custom implementation such as stringhold's StringHolderReaderTransformer,
the number of actual `toString` conversions can be as low as 1, and — depending on specific
implementation tricks (such as reusing instances of identical partial sequences) — a significantly
lower memory footprint can be observed (in this case, with stringhold, 7x less usage). If the
data is to be written to a Writer (instead of serializing to a complete string), an even lower
memory footprint can be achieved (35x less usage was observed).
RenderTransformerDefaultImpl,
stringhold| Modifier and Type | Interface and Description |
|---|---|
static interface |
RenderTransformer.ObjectAppender
Something that can append objects.
|
| Modifier and Type | Method and Description |
|---|---|
RenderTransformer.ObjectAppender.Controller |
newObjectAppender(TemplateContext context,
int estimatedNumberOfAppends)
Creates a new
RenderTransformer.ObjectAppender.Controller for the given
TemplateContext, optionally using the given estimate for the number of calls to
RenderTransformer.ObjectAppender.append(Object). |
java.lang.Object |
transformObject(TemplateContext context,
java.lang.Object obj)
Transforms an object to a representation that is suitable to call
Object.toString() on
during the "render"-phase (Template.render() etc.). |
RenderTransformer.ObjectAppender.Controller newObjectAppender(TemplateContext context, int estimatedNumberOfAppends)
RenderTransformer.ObjectAppender.Controller for the given
TemplateContext, optionally using the given estimate for the number of calls to
RenderTransformer.ObjectAppender.append(Object).context - The template context.estimatedNumberOfAppends - The estimate number of calls to RenderTransformer.ObjectAppender.append(Object).java.lang.Object transformObject(TemplateContext context, java.lang.Object obj)
Object.toString() on
during the "render"-phase (Template.render() etc.).context - The template context.obj - The object to transform.Copyright © 2012–2023. All rights reserved.