|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object freemarker.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 a TemplateFactory
object, in which case it is compiled immediately. Once compiled,
the resulting 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
,
TemplateFactory
,
Serialized FormNested Class Summary |
---|
Nested classes/interfaces inherited from interface freemarker.template.TemplateProcessor |
---|
TemplateProcessor.ExitStatus |
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<java.lang.String,TemplateFunctionModel> |
functions
A mapping of all function models in this compiled template. |
Fields inherited from class freemarker.template.AbstractTemplate |
---|
cache |
Constructor Summary | |
---|---|
Template(java.io.Reader reader)
Constructs a template by compiling it from a Reader. |
|
Template(java.io.Reader reader,
Cache cache)
Constructs a template by compiling it from a Reader. |
Method Summary | |
---|---|
void |
addFunction(java.lang.String name,
TemplateFunctionModel function)
Adds a function to the template. |
java.lang.Object |
clone()
Clones the current template. |
protected void |
compile(java.io.Reader reader)
Compiles the template from a 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,
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<java.lang.String> |
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 . |
TemplateProcessor.ExitStatus |
process(TemplateProcessorParameters p)
Processes the contents of this TemplateProcessor and
outputs the resulting text to a Writer . |
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 . |
TemplateProcessor.ExitStatus |
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 |
---|
getCache, getTemplateText |
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<java.lang.String,TemplateFunctionModel> functions
protected transient ParseException buildError
Constructor Detail |
---|
public Template(java.io.Reader reader) throws java.io.IOException
compile()
.
reader
- the source of the template file to be compiled.
java.io.IOException
public Template(java.io.Reader reader, Cache cache) throws java.io.IOException
compile()
.
reader
- the source of the template file to be compiled.
java.io.IOException
Method Detail |
---|
protected void compile(java.io.Reader reader) throws java.io.IOException, java.lang.IllegalArgumentException
Reader
.
compile
in class AbstractTemplate
reader
- a Reader
from which the
template can be read.
java.io.IOException
java.lang.IllegalArgumentException
protected TemplateProcessor compileText(java.lang.String text) throws java.io.IOException
text
- 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 FunctionTemplateProcessor
name
- 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)
CallInstruction
s
and IncludeInstruction
s at run-time.
getFunction
in interface FunctionTemplateProcessor
name
- the name of the function to be retrievedpublic java.util.Set<java.lang.String> 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(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 TemplateProcessor.ExitStatus process(TemplateWriteableHashModel modelRoot, java.io.Writer out, TemplateRuntimeHandler eventHandler) throws java.io.IOException
TemplateProcessor
and
outputs the resulting text to a Writer
.
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 TemplateProcessor.ExitStatus process(TemplateProcessorParameters p) throws java.io.IOException
TemplateProcessor
and
outputs the resulting text to a Writer
.
process
in interface TemplateProcessor
process
in class AbstractTemplate
p
- template processing parameters
java.io.IOException
- an IO error occurred with the Writer
during processingpublic void process(TemplateWriteableHashModel 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.
java.io.IOException
public void process(java.io.Writer out) throws java.io.IOException
Writer
.
process
in class AbstractTemplate
out
- a Writer
to output the text to.
java.io.IOException
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.
clone
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 |