public abstract class BaseTemplate extends java.lang.Object implements Writable
All templates compiled through MarkupTemplateEngine
extend this abstract class,
which provides a number of utility methods to generate markup. An instance of this class can be obtained
after calling Template.make()
or Template.make(java.util.Map)
)} on
a template generated by MarkupTemplateEngine.createTemplate(java.io.Reader)
.
It is advised to use a distinct template instance for each thread (or more simply, each rendered document) for thread safety and avoiding mixing models.
For the application needs, it is possible to provide more helper methods by extending this class and
configuring the base template class using the TemplateConfiguration.setBaseTemplateClass(Class)
method.
Constructor and Description |
---|
BaseTemplate(MarkupTemplateEngine templateEngine,
java.util.Map model,
java.util.Map<java.lang.String,java.lang.String> modelTypes,
TemplateConfiguration configuration) |
Modifier and Type | Method and Description |
---|---|
BaseTemplate |
comment(java.lang.Object cs)
Renders the supplied object using its
Object.toString() method inside a
comment markup block (<!-- ... |
Closure |
contents(Closure cl)
Wraps a closure so that it can be used as a prototype for inclusion in layouts.
|
java.lang.Object |
fragment(java.util.Map model,
java.lang.String templateText)
Renders an embedded template as a fragment.
|
java.util.Map |
getModel() |
java.io.Writer |
getOut()
Convenience method to return the current writer instance.
|
void |
includeEscaped(java.lang.String templatePath)
Includes contents of another file, not as a template but as escaped text.
|
void |
includeGroovy(java.lang.String templatePath)
Includes another template inside this template.
|
void |
includeUnescaped(java.lang.String templatePath)
Includes contents of another file, not as a template but as unescaped text.
|
java.lang.Object |
layout(java.util.Map model,
java.lang.String templateName)
Imports a template and renders it using the specified model, allowing fine grained composition
of templates and layouting.
|
java.lang.Object |
layout(java.util.Map model,
java.lang.String templateName,
boolean inheritModel)
Imports a template and renders it using the specified model, allowing fine grained composition of templates and
layouting.
|
java.lang.Object |
methodMissing(java.lang.String tagName,
java.lang.Object args)
This is the main method responsible for writing a tag and its attributes.
|
void |
newLine()
Adds a new line to the output.
|
BaseTemplate |
pi(java.util.Map<?,?> attrs)
Renders processing instructions.
|
abstract java.lang.Object |
run() |
java.lang.String |
stringOf(Closure cl) |
java.lang.String |
toString() |
java.lang.Object |
tryEscape(java.lang.Object contents)
Escapes the string representation of the supplied object if it derives from
CharSequence ,
otherwise returns the object itself. |
java.io.Writer |
writeTo(java.io.Writer out)
Main method used to render a template.
|
BaseTemplate |
xmlDeclaration()
Renders an XML declaration header.
|
BaseTemplate |
yield(java.lang.Object obj)
Renders the object provided as parameter using its
Object.toString() method,
The contents is rendered after being escaped for XML, enforcing valid XML output. |
BaseTemplate |
yieldUnescaped(java.lang.Object obj)
Renders the object provided as parameter using its
Object.toString() method,
The contents is rendered as is, unescaped. |
public BaseTemplate(MarkupTemplateEngine templateEngine, java.util.Map model, java.util.Map<java.lang.String,java.lang.String> modelTypes, TemplateConfiguration configuration)
public java.util.Map getModel()
public abstract java.lang.Object run()
public BaseTemplate yieldUnescaped(java.lang.Object obj) throws java.io.IOException
Object.toString()
method,
The contents is rendered as is, unescaped. This means that depending on what the
Object.toString()
method call returns, you might create invalid markup.obj
- the object to be rendered unescapedjava.io.IOException
public BaseTemplate yield(java.lang.Object obj) throws java.io.IOException
Object.toString()
method,
The contents is rendered after being escaped for XML, enforcing valid XML output.obj
- the object to be renderedjava.io.IOException
public java.lang.String stringOf(Closure cl) throws java.io.IOException
java.io.IOException
public BaseTemplate comment(java.lang.Object cs) throws java.io.IOException
Object.toString()
method inside a
comment markup block (<!-- ... -->). The object is rendered as is, unescaped.cs
- the object to be rendered inside an XML comment block.java.io.IOException
public BaseTemplate xmlDeclaration() throws java.io.IOException
template configuration
,
then the encoding is rendered into the declaration.java.io.IOException
public BaseTemplate pi(java.util.Map<?,?> attrs) throws java.io.IOException
Renders processing instructions. The supplied map contains all elements to be rendered as processing instructions. The key is the name of the element, the value is either a map of attributes, or an object to be rendered directly. For example:
pi("xml-stylesheet":[href:"mystyle.css", type:"text/css"])
will be rendered as:
<?xml-stylesheet href='mystyle.css' type='text/css'?>
attrs
- the attributes to renderjava.io.IOException
public java.lang.Object methodMissing(java.lang.String tagName, java.lang.Object args) throws java.io.IOException
or a combination of (attributes,string), (attributes,closure)
tagName
- the name of the tagargs
- tag generation argumentsjava.io.IOException
public void includeGroovy(java.lang.String templatePath) throws java.io.IOException, java.lang.ClassNotFoundException
templatePath
- the path to the included resource.java.io.IOException
java.lang.ClassNotFoundException
public void includeEscaped(java.lang.String templatePath) throws java.io.IOException
templatePath
- the path to the other filejava.io.IOException
public void includeUnescaped(java.lang.String templatePath) throws java.io.IOException
templatePath
- the path to the other filejava.io.IOException
public java.lang.Object tryEscape(java.lang.Object contents)
CharSequence
,
otherwise returns the object itself.contents
- an object to be escaped for XMLpublic java.io.Writer getOut()
public void newLine() throws java.io.IOException
TemplateConfiguration.setNewLineString(String)
java.io.IOException
public java.lang.Object fragment(java.util.Map model, java.lang.String templateText) throws java.io.IOException, java.lang.ClassNotFoundException
layout(java.util.Map, String)
.model
- model to be passed to the templatetemplateText
- template bodyjava.io.IOException
java.lang.ClassNotFoundException
public java.lang.Object layout(java.util.Map model, java.lang.String templateName) throws java.io.IOException, java.lang.ClassNotFoundException
layout(java.util.Map, String, boolean)
.model
- model to be passed to the templatetemplateName
- the name of the template to be used as a layoutjava.io.IOException
java.lang.ClassNotFoundException
public java.lang.Object layout(java.util.Map model, java.lang.String templateName, boolean inheritModel) throws java.io.IOException, java.lang.ClassNotFoundException
model
- model to be passed to the templatetemplateName
- the name of the template to be used as a layoutinheritModel
- a boolean indicating if we should inherit the parent modeljava.io.IOException
java.lang.ClassNotFoundException
public Closure contents(Closure cl)
cl
- the fragment to be wrappedpublic java.io.Writer writeTo(java.io.Writer out) throws java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object