|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectfreemarker.template.AbstractTemplate
freemarker.template.Template
public class Template
An application or servlet can instantiate a Template to compile
and process an HTML template.
You can pass the filename of the template to the constructor, in which case it is compiled immediately. Once compiled, the template is stored in an an efficient data structure for later use.
To process the template and produce HTML, call the
process(TemplateWriteableHashModel, Writer) method, which takes a
tree of TemplateModel objects as its data model. The root node of the
tree must be a TemplateWriteableHashModel.
Any error messages from exceptions thrown by the data model, or generated by
the Template during compilation or processing, will be included
as HTML comments in the output.
To facilitate multithreading, Template objects are immutable; if
you need to recompile a template, you must make a new Template
object. In most cases, it will be sufficient to let a TemplateCache
do this for you.
TemplateCache,
Serialized Form| Field Summary | |
|---|---|
protected ParseException |
buildError
A cached copy of any parser exception thrown during compilation. |
protected TemplateProcessor |
compiledTemplate
The root node of the compiled template. |
protected java.util.Map |
functions
A mapping of all function models in this compiled template. |
| Fields inherited from class freemarker.template.AbstractTemplate |
|---|
cache |
| Fields inherited from interface freemarker.template.TemplateProcessor |
|---|
BREAK, EXIT, OK, UNCOMPILED_TEMPLATE |
| Constructor Summary | |
|---|---|
Template()
Constructs an empty template. |
|
Template(java.io.File file)
Deprecated. use the InputSource contructor to supply source
streams to the template compiler |
|
Template(InputSource source)
Constructs a template by compiling it from an InputSource. |
|
Template(java.io.InputStream stream)
Deprecated. use the InputSource contructor to supply source
streams to the template compiler |
|
Template(java.io.Reader stream)
Deprecated. use the InputSource contructor to supply source
streams to the template compiler |
|
Template(java.lang.String filePath)
Deprecated. use the InputSource contructor to supply source
streams to the template compiler |
|
| Method Summary | |
|---|---|
void |
addFunction(java.lang.String name,
TemplateFunctionModel function)
Adds a function to the template. |
java.lang.Object |
clone()
Clones the current template. |
void |
compile(InputSource source)
Compiles the template from an InputSource. |
protected TemplateProcessor |
compileText(InputSource source)
Compiles the template text using the standard parser and builder classes. |
protected static void |
copyFunctions(FunctionTemplateProcessor template,
TemplateWriteableHashModel modelRoot)
Copies functions from a template into a data model. |
TemplateFunctionModel |
getFunction(java.lang.String name)
Retrieves a function from the template. |
java.util.Set |
getFunctionNames()
Retrieve a Set of function names for this template. |
void |
process(java.io.PrintWriter out)
Processes the template, using an empty data model, and outputs the resulting text to a PrintWriter. |
void |
process(TemplateWriteableHashModel modelRoot,
java.io.PrintWriter out)
Processes the template, using data from a template model, and outputs the resulting text to a PrintWriter. |
void |
process(TemplateWriteableHashModel modelRoot,
java.io.PrintWriter out,
TemplateRuntimeHandler eventHandler)
Processes the contents of this TemplateProcessor and outputs the
resulting text to a PrintWriter. |
void |
process(TemplateWriteableHashModel modelRoot,
java.io.Writer out)
Processes the template, using data from a template model, and outputs the resulting text to a Writer. |
short |
process(TemplateWriteableHashModel modelRoot,
java.io.Writer out,
TemplateRuntimeHandler eventHandler)
Processes the contents of this TemplateProcessor and outputs the
resulting text to a Writer. |
void |
process(java.io.Writer out)
Processes the template, using an empty data model, and outputs the resulting text to a Writer. |
java.lang.String |
toString()
Returns a string representation of the object. |
| Methods inherited from class freemarker.template.AbstractTemplate |
|---|
compileFromFile, compileFromFile, compileFromStream, compileFromStream, compileFromStream, getCache, setCache |
| Methods inherited from class java.lang.Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected TemplateProcessor compiledTemplate
protected java.util.Map functions
protected transient ParseException buildError
| Constructor Detail |
|---|
public Template()
public Template(InputSource source)
throws java.io.IOException
compile().
source - the source of the template file to be compiled.
java.io.IOException
public Template(java.lang.String filePath)
throws java.io.IOException
InputSource contructor to supply source
streams to the template compiler
compileFromFile().
filePath - the absolute path of the template file to be compiled.
java.io.IOException
public Template(java.io.File file)
throws java.io.IOException
InputSource contructor to supply source
streams to the template compiler
compileFromFile().
file - a File representing the template file to be
compiled.
java.io.IOException
public Template(java.io.InputStream stream)
throws java.io.IOException
InputSource contructor to supply source
streams to the template compiler
InputStream.
Calls compileFromStream().
stream - an InputStream from which the template can be read.
java.io.IOException
public Template(java.io.Reader stream)
throws java.io.IOException
InputSource contructor to supply source
streams to the template compiler
Reader. Calls
compileFromStream().
stream - a Reader from which the template can be read.
java.io.IOException| Method Detail |
|---|
public void compile(InputSource source)
throws java.io.IOException,
java.lang.IllegalArgumentException
InputSource. If the template
has already been compiled, this method does nothing. Calls
AbstractTemplate.compileFromStream(java.io.Reader) to perform parsing.
compile in interface Compileablecompile in class AbstractTemplatesource - an InputSource from which the template can be
read.
java.io.IOException
java.lang.IllegalArgumentException
protected TemplateProcessor compileText(InputSource source)
throws java.io.IOException
source - the text to compile.
TemplateProcessor representing the compiled
template.
java.io.IOException
public void addFunction(java.lang.String name,
TemplateFunctionModel function)
throws java.lang.IllegalStateException
TemplateBuilder at compile-time.
addFunction in interface FunctionTemplateProcessorname - the name of the function to be storedfunction - the function to be stored by the template
java.lang.IllegalStateException - the method has been called after the template has been
compiledpublic TemplateFunctionModel getFunction(java.lang.String name)
CallInstructions and IncludeInstructions at
run-time.
getFunction in interface FunctionTemplateProcessorname - the name of the function to be retrievedpublic java.util.Set getFunctionNames()
Set of function names for this template.
getFunctionNames in interface FunctionTemplateProcessorSet of function names (String
objects) that have been defined for this template.
public void process(TemplateWriteableHashModel modelRoot,
java.io.PrintWriter out,
TemplateRuntimeHandler eventHandler)
TemplateProcessor and outputs the
resulting text to a PrintWriter.
modelRoot - the root node of the data model.out - a PrintWriter to send the output to.eventHandler - a TemplateEventAdapter for handling any events
that occur during processing.
public void process(TemplateWriteableHashModel modelRoot,
java.io.PrintWriter out)
PrintWriter.
modelRoot - the root node of the data model. If null, an
empty data model is used.out - a PrintWriter to send the output to.public void process(java.io.PrintWriter out)
PrintWriter.
out - a PrintWriter to send the output to.
public short process(TemplateWriteableHashModel modelRoot,
java.io.Writer out,
TemplateRuntimeHandler eventHandler)
throws java.io.IOException
TemplateProcessor and outputs the
resulting text to a Writer.
process in interface TemplateProcessorprocess in class AbstractTemplatemodelRoot - the root node of the data model.out - a Writer to send the output to.eventHandler - a TemplateEventAdapter for handling any events
that occur during processing.
java.io.IOException - an IO error occurred with the Writer during
processing
public void process(TemplateWriteableHashModel modelRoot,
java.io.Writer out)
throws java.io.IOException
Writer.
process in class AbstractTemplatemodelRoot - the root node of the data model. If null, an
empty data model is used.out - a Writer to output the text to.
java.io.IOException
public void process(java.io.Writer out)
throws java.io.IOException
Writer.
process in class AbstractTemplateout - a Writer to output the text to.
java.io.IOExceptionpublic java.lang.Object clone()
Clones the current template. A shallow clone is performed, meaning that changes to the underlying structure of one will affect the structure of the other. As Templates are immutable anyway, this shouldn't be an issue.
Cloning is used in Caches, whenever we
need to create a new template: rather than simply creating a new
Template, we ask a
TemplateRegistry to create one for us.
TemplateRegistry uses the clone function to take an existing
template, copy it, and return the copy to the cache, where it is then
populated.
clone in interface Cacheableclone in class AbstractTemplate
protected static void copyFunctions(FunctionTemplateProcessor template,
TemplateWriteableHashModel modelRoot)
throws TemplateModelException
template - the template from which function instructions will be copiedmodelRoot - the template model where functions will be copied to
TemplateModelException - the model could not accept the functionpublic java.lang.String toString()
toString in class java.lang.Object
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||