|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--freemarker.template.AbstractTemplate | +--freemarker.template.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(TemplateModelRoot, Writer)
method, which takes a tree of
TemplateModel
objects as its data model. The root
node of the tree must be a TemplateModelRoot
.
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 FormField 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 |
Constructor Summary | |
Template()
Constructs an empty template. |
|
Template(java.io.File file)
Constructs a template by compiling it from a file. |
|
Template(java.io.InputStream stream)
Constructs a template by compiling it from an InputStream . |
|
Template(java.io.Reader stream)
Constructs a template by compiling it from an Reader . |
|
Template(java.lang.String filePath)
Constructs a template by compiling it from a file. |
Method Summary | |
void |
addFunction(FunctionInstruction function)
Adds a function to the template. |
java.lang.Object |
clone()
Clones the current template. |
void |
compileFromStream(java.io.Reader stream)
Compiles the template from an Reader . |
protected TemplateProcessor |
compileText(java.lang.String text)
Compiles the template text using the standard parser and builder classes. |
protected static void |
copyFunctions(FunctionTemplateProcessor template,
TemplateModelRoot modelRoot)
Copies functions from a template into a data model. |
FunctionInstruction |
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(TemplateModelRoot modelRoot,
java.io.PrintWriter out)
Processes the template, using data from a template model, and outputs the resulting text to a PrintWriter . |
void |
process(TemplateModelRoot modelRoot,
java.io.PrintWriter out,
TemplateRuntimeHandler eventHandler)
Processes the contents of this TemplateProcessor and
outputs the resulting text to a PrintWriter . |
void |
process(TemplateModelRoot modelRoot,
java.io.Writer out)
Processes the template, using data from a template model, and outputs the resulting text to a Writer . |
void |
process(TemplateModelRoot 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, 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(java.lang.String filePath) throws java.io.IOException
compileFromFile()
.filePath
- the absolute path of the template file
to be compiled.public Template(java.io.File file) throws java.io.IOException
compileFromFile()
.file
- a File
representing the
template file to be compiled.public Template(java.io.InputStream stream) throws java.io.IOException
InputStream
. Calls
compileFromStream()
.stream
- an InputStream from which the
template can be read.public Template(java.io.Reader stream) throws java.io.IOException
Reader
. Calls compileFromStream()
.stream
- a Reader
from which the
template can be read.Method Detail |
public void compileFromStream(java.io.Reader stream) throws java.io.IOException
Reader
. If the template has
already been compiled, this method does nothing.compileFromStream
in class AbstractTemplate
stream
- a Reader
from which the
template can be read.protected TemplateProcessor compileText(java.lang.String text)
text
- the text to compile.TemplateProcessor
representing the compiled template.public void addFunction(FunctionInstruction function)
TemplateBuilder
at compile-time.addFunction
in interface FunctionTemplateProcessor
function
- the function to be stored by the templatepublic FunctionInstruction getFunction(java.lang.String name)
CallInstruction
s
and IncludeInstruction
s at run-time.getFunction
in interface FunctionTemplateProcessor
name
- the name of the function to be retrievedpublic java.util.Set getFunctionNames()
Set
of function names for this template.getFunctionNames
in interface FunctionTemplateProcessor
Set
of function names (String
objects)
that have been defined for this template.public void process(TemplateModelRoot 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(TemplateModelRoot 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 void process(TemplateModelRoot modelRoot, java.io.Writer out, TemplateRuntimeHandler eventHandler) throws java.io.IOException
TemplateProcessor
and
outputs the resulting text to a Writer
.process
in interface TemplateProcessor
process
in class AbstractTemplate
modelRoot
- 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 processingpublic void process(TemplateModelRoot modelRoot, java.io.Writer out) throws java.io.IOException
Writer
.process
in class AbstractTemplate
modelRoot
- the root node of the data model. If null
,
an empty data model is used.out
- a Writer
to output the text to.public void process(java.io.Writer out) throws java.io.IOException
Writer
.process
in class AbstractTemplate
out
- a Writer
to output the text to.public 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 Cache
s, 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 class AbstractTemplate
protected static void copyFunctions(FunctionTemplateProcessor template, TemplateModelRoot modelRoot)
template
- the template from which function instructions will
be copiedmodelRoot
- the template model where functions will be copied topublic java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |