public final class Template extends Object
fromTemplate(VelocityEngine, String)
and fromTemplate(VelocityEngine, String, Charset)
,
for evaluating literal templates and fromTemplateName(VelocityEngine, String)
and
fromTemplateName(VelocityEngine, String, Charset)
for evaluating templates referenced by name. It also
ensures that the given VelocityEngine
is configured in such a way as to be able to use the literal or named
template. Note, this check occurs only at Template
construction time so, if you're loading a named template
from a file and that file disappears before calling merge(Context)
or merge(Context, Writer)
you'll
still end up getting a ResourceNotFoundException
.
Many methods throw an VelocityException
to report template or argument errors,
which is an unchecked exception type.Modifier and Type | Field and Description |
---|---|
private org.apache.velocity.app.VelocityEngine |
engine
The
VelocityEngine used when evaluating the template. |
private String |
templateEncoding
The character encoding of the template.
|
private String |
templateName
The name of the template to be evaluated.
|
Modifier | Constructor and Description |
---|---|
private |
Template(org.apache.velocity.app.VelocityEngine velocityEngine,
String velocityTemplateName,
String velocityTemplateEncoding)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj) |
static Template |
fromTemplate(org.apache.velocity.app.VelocityEngine engine,
String template)
A convenience method that invoked
fromTemplate(VelocityEngine, String, Charset) and assumes the given
template is US ASCII encoded. |
static Template |
fromTemplate(org.apache.velocity.app.VelocityEngine engine,
String template,
Charset encoding)
Constructs a
Template from a given template. |
static Template |
fromTemplateName(org.apache.velocity.app.VelocityEngine engine,
String templateName)
A convenience method that invoked
fromTemplateName(VelocityEngine, String, Charset) and assumes the
named template is US ASCII encoded. |
static Template |
fromTemplateName(org.apache.velocity.app.VelocityEngine engine,
String name,
Charset encoding)
Constructs a
Template that evaluates a named velocity template with a using the given velocity engine. |
String |
getTemplateName()
Gets the name of the template.
|
int |
hashCode() |
String |
merge(org.apache.velocity.context.Context templateContext)
Evaluates the template using the given context and returns the result as a string.
|
void |
merge(org.apache.velocity.context.Context templateContext,
Writer output)
Evaluates the template using the given context and sends the result to a Writer.
|
String |
toString() |
private final org.apache.velocity.app.VelocityEngine engine
VelocityEngine
used when evaluating the template.private final String templateName
private final String templateEncoding
private Template(@Nonnull org.apache.velocity.app.VelocityEngine velocityEngine, @Nonnull @NotEmpty String velocityTemplateName, @Nonnull String velocityTemplateEncoding)
velocityEngine
- engine used to evaluate the templatevelocityTemplateName
- name of the template to be evaluatedvelocityTemplateEncoding
- encoding used by the template@Nonnull public static Template fromTemplate(@Nonnull org.apache.velocity.app.VelocityEngine engine, @Nonnull @NotEmpty String template)
fromTemplate(VelocityEngine, String, Charset)
and assumes the given
template is US ASCII encoded.
See fromTemplate(VelocityEngine, String, Charset)
for full details.
engine
- engine that will be used to evaluate the templatetemplate
- the literal Velocity template, NOT a template name see
fromTemplateName(VelocityEngine, String)
and
fromTemplateName(VelocityEngine, String, Charset)
for that@Nonnull public static Template fromTemplate(@Nonnull org.apache.velocity.app.VelocityEngine engine, @Nonnull @NotEmpty String template, @Nonnull Charset encoding)
Template
from a given template. This template is loaded in to the singleton
StringResourceRepository
used by the StringResourceLoader
under a randomly generated ID.
Therefore, calling this method multiple times with the same template will result in multiple instances of the
template string being loaded in to the StringResourceRepository
(each under its own unique ID).
NOTE, in oder for subsequent calls to merge(Context)
or merge(Context, Writer)
to be successful, the given VelocityEngine
must be configured to look up templates from the
StringResourceLoader
.
engine
- engine that will be used to evaluate the templatetemplate
- the literal Velocity template, NOT a template name see
fromTemplateName(VelocityEngine, String)
or
fromTemplateName(VelocityEngine, String, Charset)
for thatencoding
- the encoding used by the templatepublic static Template fromTemplateName(@Nonnull org.apache.velocity.app.VelocityEngine engine, @Nonnull @NotEmpty String templateName)
fromTemplateName(VelocityEngine, String, Charset)
and assumes the
named template is US ASCII encoded.
See fromTemplateName(VelocityEngine, String, Charset)
for full details.
engine
- engine that will be used to evaluate the templatetemplateName
- the name, as known to the given engine, of a velocity templatepublic static Template fromTemplateName(@Nonnull org.apache.velocity.app.VelocityEngine engine, @Nonnull @NotEmpty String name, @Nonnull Charset encoding)
Template
that evaluates a named velocity template with a using the given velocity engine.engine
- the engine used to evaluate the templatename
- the name of the templateencoding
- the template encoding@Nonnull public String getTemplateName()
public String merge(org.apache.velocity.context.Context templateContext)
templateContext
- current template contextpublic void merge(org.apache.velocity.context.Context templateContext, Writer output)
templateContext
- current template contextoutput
- writer that will receive the template outputCopyright © 1999–2018. All rights reserved.