Top Description Inners Fields Constructors Methods
groovy.text.markup

public abstract Class BaseTemplate

extends Object
implements Writable
Class Inheritance
All Implemented Interfaces
groovy.lang.Writable
Imports
groovy.lang.Closure, .Writable, groovy.text.Template, org.apache.groovy.internal.util.UncheckedThrow, org.apache.groovy.io.StringBuilderWriter, org.codehaus.groovy.control.io.NullWriter, org.codehaus.groovy.runtime.ResourceGroovyMethods, java.io.IOException, .StringReader, .Writer, java.net.URL, java.util.Collections, .HashMap, .LinkedHashMap, .Map

All templates compiled through groovy.text.markup.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 groovy.text.Template#make() or groovy.text.Template#make(java.util.Map))} on a template generated by groovy.text.markup.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 groovy.text.markup.TemplateConfiguration#setBaseTemplateClass(Class) method.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final Map<String, Template>
private final TemplateConfiguration
private boolean
private static final Map<K, V>
private final MarkupTemplateEngine
private final Map<K, V>
private final Map<String, String>
private Writer

Constructor Summary

AccessConstructor and Description
public
BaseTemplate(final MarkupTemplateEngine templateEngine, final Map<K, V> model, final Map<String, String> modelTypes, final TemplateConfiguration configuration)

Method Summary

Modifier and TypeMethod and Description
public BaseTemplate

Returns:

this template instance.
comment
(Object
the object to be rendered inside an XML comment block.
cs
)

Renders the supplied object using its Object#toString method inside a comment markup block (<!-- ...

public Closure<V>

Returns:

a wrapped closure returning an empty string
contents
(final Closure<V>
the fragment to be wrapped
cl
)

Wraps a closure so that it can be used as a prototype for inclusion in layouts.

private Writer
createWriter(final Writer out)

private String
private Map<K, V>
forkModel(Map<K, V> m)

public Object

Returns:

this template instance
fragment
(Map<K, V>
model to be passed to the template
model
,
String
template body
templateText
)

Renders an embedded template as a fragment.

public Map<K, V>
public Writer

Returns:

the current writer
getOut
()

Convenience method to return the current writer instance.

public void
includeEscaped(String
the path to the other file
templatePath
)

Includes contents of another file, not as a template but as escaped text.

public void
includeGroovy(String
the path to the included resource.
templatePath
)

Includes another template inside this template.

public void
includeUnescaped(String
the path to the other file
templatePath
)

Includes contents of another file, not as a template but as unescaped text.

public Object

Returns:

this template instance
layout
(Map<K, V>
model to be passed to the template
model
,
String
the name of the template to be used as a layout
templateName
)

Imports a template and renders it using the specified model, allowing fine grained composition of templates and layouting.

public Object

Returns:

this template instance
layout
(Map<K, V>
model to be passed to the template
model
,
String
the name of the template to be used as a layout
templateName
,
boolean
a boolean indicating if we should inherit the parent model
inheritModel
)

Imports a template and renders it using the specified model, allowing fine grained composition of templates and layouting.

public Object

Returns:

this template instance
methodMissing
(String
the name of the tag
tagName
,
Object
tag generation arguments
args
)

This is the main method responsible for writing a tag and its attributes.

public void
newLine()

Adds a new line to the output.

public BaseTemplate

Returns:

this template instance
pi
(Map<?, ?>
the attributes to render
attrs
)

Renders processing instructions.

public abstract Object
run()

public String
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public Object

Returns:

an escaped string, or the object itself
tryEscape
(Object
an object to be escaped for XML
contents
)

Escapes the string representation of the supplied object if it derives from java.lang.CharSequence, otherwise returns the object itself.

private void
writeAttribute(String attName, String value)

private void
writeAttributes(final Map<?, ?> attributes)

private void
writeBody(final Object body)

private void
private void
public Writer

Returns:

a writer instance
writeTo
(final Writer
the Writer to which this Writable should output its data.
out
)

Implements groovy.lang.Writable.writeTo.

Main method used to render a template.

public BaseTemplate

Returns:

this template instance
xmlDeclaration
()

Renders an XML declaration header.

public BaseTemplate

Returns:

this template instance
yield
(Object
the object to be rendered
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.

public BaseTemplate

Returns:

this template instance
yieldUnescaped
(Object
the object to be rendered unescaped
obj
)

Renders the object provided as parameter using its Object#toString() method, The contents is rendered as is, unescaped.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

cachedFragmentsback to summary
private final Map<String, Template> cachedFragments
configurationback to summary
private final TemplateConfiguration configuration
doWriteIndentback to summary
private boolean doWriteIndent
EMPTY_MODELback to summary
private static final Map<K, V> EMPTY_MODEL
engineback to summary
private final MarkupTemplateEngine engine
modelback to summary
private final Map<K, V> model
modelTypesback to summary
private final Map<String, String> modelTypes
outback to summary
private Writer out

Constructor Detail

BaseTemplateback to summary
public BaseTemplate(final MarkupTemplateEngine templateEngine, final Map<K, V> model, final Map<String, String> modelTypes, final TemplateConfiguration configuration)

Method Detail

commentback to summary
public BaseTemplate comment(Object cs) throws IOException

Renders the supplied object using its Object#toString method inside a comment markup block (<!-- ... -->). The object is rendered as is, unescaped.

Parameters
cs:Object

the object to be rendered inside an XML comment block.

Returns:BaseTemplate

this template instance.

contentsback to summary
public Closure<V> contents(final Closure<V> cl)

Wraps a closure so that it can be used as a prototype for inclusion in layouts. This is useful when you want to use a closure in a model, but that you don't want to render the result of the closure but instead call it as if it was a specification of a template fragment.

Parameters
cl:Closure<V>

the fragment to be wrapped

Returns:Closure<V>

a wrapped closure returning an empty string

createWriterback to summary
private Writer createWriter(final Writer out)
escapeQuotesback to summary
private String escapeQuotes(String str)
forkModelback to summary
private Map<K, V> forkModel(Map<K, V> m)
Annotations
@SuppressWarnings:unchecked
fragmentback to summary
public Object fragment(Map<K, V> model, String templateText) throws IOException, ClassNotFoundException

Renders an embedded template as a fragment. Fragments are cached in a template, meaning that if you use the same fragment in a template, it will only be compiled once, but once per template instance. This is less performant than using layout(java.util.Map, String).

Parameters
model:Map<K, V>

model to be passed to the template

templateText:String

template body

Returns:Object

this template instance

getModelback to summary
public Map<K, V> getModel()
getOutback to summary
public Writer getOut()

Convenience method to return the current writer instance.

Returns:Writer

the current writer

includeEscapedback to summary
public void includeEscaped(String templatePath) throws IOException

Includes contents of another file, not as a template but as escaped text.

Parameters
templatePath:String

the path to the other file

includeGroovyback to summary
public void includeGroovy(String templatePath) throws IOException, ClassNotFoundException

Includes another template inside this template.

Parameters
templatePath:String

the path to the included resource.

includeUnescapedback to summary
public void includeUnescaped(String templatePath) throws IOException

Includes contents of another file, not as a template but as unescaped text.

Parameters
templatePath:String

the path to the other file

layoutback to summary
public Object layout(Map<K, V> model, String templateName) throws IOException, ClassNotFoundException

Imports a template and renders it using the specified model, allowing fine grained composition of templates and layouting. This works similarily to a template include but allows a distinct model to be used. This version doesn't inherit the model from the parent. If you need model inheritance, see layout(java.util.Map, String, boolean).

Parameters
model:Map<K, V>

model to be passed to the template

templateName:String

the name of the template to be used as a layout

Returns:Object

this template instance

layoutback to summary
public Object layout(Map<K, V> model, String templateName, boolean inheritModel) throws IOException, ClassNotFoundException

Imports a template and renders it using the specified model, allowing fine grained composition of templates and layouting. This works similarily to a template include but allows a distinct model to be used. If the layout inherits from the parent model, a new model is created, with the values from the parent model, eventually overridden with those provided specifically for this layout.

Parameters
model:Map<K, V>

model to be passed to the template

templateName:String

the name of the template to be used as a layout

inheritModel:boolean

a boolean indicating if we should inherit the parent model

Returns:Object

this template instance

methodMissingback to summary
public Object methodMissing(String tagName, Object args) throws IOException

This is the main method responsible for writing a tag and its attributes. The arguments may be:

  • a closure
  • in which case the closure is rendered inside the tag body
  • a string
  • , in which case the string is rendered as the tag body
  • a map of attributes
  • in which case the attributes are rendered inside the opening tag

or a combination of (attributes,string), (attributes,closure)

Parameters
tagName:String

the name of the tag

args:Object

tag generation arguments

Returns:Object

this template instance

newLineback to summary
public void newLine() throws IOException

Adds a new line to the output. The new line string can be configured by groovy.text.markup.TemplateConfiguration#setNewLineString(String)

piback to summary
public 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'?>
Parameters
attrs:Map<?, ?>

the attributes to render

Returns:BaseTemplate

this template instance

runback to summary
public abstract Object run()
stringOfback to summary
public String stringOf(Closure<V> cl) throws IOException
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

tryEscapeback to summary
public Object tryEscape(Object contents)

Escapes the string representation of the supplied object if it derives from java.lang.CharSequence, otherwise returns the object itself.

Parameters
contents:Object

an object to be escaped for XML

Returns:Object

an escaped string, or the object itself

writeAttributeback to summary
private void writeAttribute(String attName, String value) throws IOException
writeAttributesback to summary
private void writeAttributes(final Map<?, ?> attributes) throws IOException
writeBodyback to summary
private void writeBody(final Object body) throws IOException
writeIndentback to summary
private void writeIndent() throws IOException
writeQtback to summary
private void writeQt() throws IOException
writeToback to summary
public Writer writeTo(final Writer out) throws IOException

Implements groovy.lang.Writable.writeTo.

Main method used to render a template.

Parameters
out:Writer

the Writer to which this Writable should output its data.

Returns:Writer

a writer instance

Exceptions
IOException:

Doc from groovy.lang.Writable.writeTo.

if an error occurred while outputting data to the writer

xmlDeclarationback to summary
public BaseTemplate xmlDeclaration() throws IOException

Renders an XML declaration header. If the declaration encoding is set in the template configuration, then the encoding is rendered into the declaration.

Returns:BaseTemplate

this template instance

yieldback to summary
public BaseTemplate yield(Object obj) throws IOException

Renders the object provided as parameter using its Object#toString() method, The contents is rendered after being escaped for XML, enforcing valid XML output.

Parameters
obj:Object

the object to be rendered

Returns:BaseTemplate

this template instance

yieldUnescapedback to summary
public BaseTemplate yieldUnescaped(Object obj) throws IOException

Renders the object provided as parameter using its 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.

Parameters
obj:Object

the object to be rendered unescaped

Returns:BaseTemplate

this template instance

groovy.text.markup back to summary

private Class BaseTemplate.TagData

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final Object[]
private Map<K, V>
private Object

Constructor Summary

AccessConstructor and Description
public
TagData(final Object args)

Method Summary

Modifier and TypeMethod and Description
public Map<K, V>
public Object
public BaseTemplate.TagData
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

arrayback to summary
private final Object[] array
attributesback to summary
private Map<K, V> attributes
bodyback to summary
private Object body

Constructor Detail

TagDataback to summary
public TagData(final Object args)

Method Detail

getAttributesback to summary
public Map<K, V> getAttributes()
getBodyback to summary
public Object getBody()
invokeback to summary
public BaseTemplate.TagData invoke()