public abstract class BaseTemplate extends Object implements groovy.lang.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,
Map model,
Map<String,String> modelTypes,
TemplateConfiguration configuration) |
| Modifier and Type | Method and Description |
|---|---|
BaseTemplate |
comment(Object cs)
Renders the supplied object using its
Object.toString() method inside a
comment markup block (<!-- ... |
groovy.lang.Closure |
contents(groovy.lang.Closure cl)
Wraps a closure so that it can be used as a prototype for inclusion in layouts.
|
Object |
fragment(Map model,
String templateText)
Renders an embedded template as a fragment.
|
Map |
getModel() |
Writer |
getOut()
Convenience method to return the current writer instance.
|
void |
includeEscaped(String templatePath)
Includes contents of another file, not as a template but as escaped text.
|
void |
includeGroovy(String templatePath)
Includes another template inside this template.
|
void |
includeUnescaped(String templatePath)
Includes contents of another file, not as a template but as unescaped text.
|
Object |
layout(Map model,
String templateName)
Imports a template and renders it using the specified model, allowing fine grained composition
of templates and layouting.
|
Object |
layout(Map model,
String templateName,
boolean inheritModel)
Imports a template and renders it using the specified model, allowing fine grained composition of templates and
layouting.
|
Object |
methodMissing(String tagName,
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(Map<?,?> attrs)
Renders processing instructions.
|
abstract Object |
run() |
String |
stringOf(groovy.lang.Closure cl) |
String |
toString() |
Object |
tryEscape(Object contents)
Escapes the string representation of the supplied object if it derives from
CharSequence,
otherwise returns the object itself. |
Writer |
writeTo(Writer out)
Main method used to render a template.
|
BaseTemplate |
xmlDeclaration()
Renders an XML declaration header.
|
BaseTemplate |
yield(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(Object obj)
Renders the object provided as parameter using its
Object.toString() method,
The contents is rendered as is, unescaped. |
public BaseTemplate(MarkupTemplateEngine templateEngine, Map model, Map<String,String> modelTypes, TemplateConfiguration configuration)
public Map getModel()
public abstract Object run()
public BaseTemplate yieldUnescaped(Object obj) throws 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 unescapedIOExceptionpublic BaseTemplate yield(Object obj) throws IOException
Object.toString() method,
The contents is rendered after being escaped for XML, enforcing valid XML output.obj - the object to be renderedIOExceptionpublic String stringOf(groovy.lang.Closure cl) throws IOException
IOExceptionpublic BaseTemplate comment(Object cs) throws 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.IOExceptionpublic BaseTemplate xmlDeclaration() throws IOException
template configuration,
then the encoding is rendered into the declaration.IOExceptionpublic BaseTemplate pi(Map<?,?> attrs) throws 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 renderIOExceptionpublic Object methodMissing(String tagName, Object args) throws IOException
or a combination of (attributes,string), (attributes,closure)
tagName - the name of the tagargs - tag generation argumentsIOExceptionpublic void includeGroovy(String templatePath) throws IOException, ClassNotFoundException
templatePath - the path to the included resource.IOExceptionClassNotFoundExceptionpublic void includeEscaped(String templatePath) throws IOException
templatePath - the path to the other fileIOExceptionpublic void includeUnescaped(String templatePath) throws IOException
templatePath - the path to the other fileIOExceptionpublic Object tryEscape(Object contents)
CharSequence,
otherwise returns the object itself.contents - an object to be escaped for XMLpublic Writer getOut()
public void newLine()
throws IOException
TemplateConfiguration.setNewLineString(String)IOExceptionpublic Object fragment(Map model, String templateText) throws IOException, ClassNotFoundException
layout(java.util.Map, String).model - model to be passed to the templatetemplateText - template bodyIOExceptionClassNotFoundExceptionpublic Object layout(Map model, String templateName) throws IOException, 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 layoutIOExceptionClassNotFoundExceptionpublic Object layout(Map model, String templateName, boolean inheritModel) throws IOException, 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 modelIOExceptionClassNotFoundExceptionpublic groovy.lang.Closure contents(groovy.lang.Closure cl)
cl - the fragment to be wrappedpublic Writer writeTo(Writer out) throws IOException
writeTo in interface groovy.lang.Writableout - the Writer to which this Writable should output its data.IOException