Class BaseTemplate
- java.lang.Object
- 
- groovy.text.markup.BaseTemplate
 
- 
- All Implemented Interfaces:
- Writable
 
 public abstract class BaseTemplate extends java.lang.Object implements Writable All templates compiled through MarkupTemplateEngineextend this abstract class, which provides a number of utility methods to generate markup. An instance of this class can be obtained after callingTemplate.make()orTemplate.make(java.util.Map))} on a template generated byMarkupTemplateEngine.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 SummaryConstructors Constructor Description BaseTemplate(MarkupTemplateEngine templateEngine, java.util.Map model, java.util.Map<java.lang.String,java.lang.String> modelTypes, TemplateConfiguration configuration)
 - 
Method SummaryAll Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description BaseTemplatecomment(java.lang.Object cs)Renders the supplied object using itsObject.toString()method inside a comment markup block (<!-- ...Closurecontents(Closure cl)Wraps a closure so that it can be used as a prototype for inclusion in layouts.java.lang.Objectfragment(java.util.Map model, java.lang.String templateText)Renders an embedded template as a fragment.java.util.MapgetModel()java.io.WritergetOut()Convenience method to return the current writer instance.voidincludeEscaped(java.lang.String templatePath)Includes contents of another file, not as a template but as escaped text.voidincludeGroovy(java.lang.String templatePath)Includes another template inside this template.voidincludeUnescaped(java.lang.String templatePath)Includes contents of another file, not as a template but as unescaped text.java.lang.Objectlayout(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.Objectlayout(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.ObjectmethodMissing(java.lang.String tagName, java.lang.Object args)This is the main method responsible for writing a tag and its attributes.voidnewLine()Adds a new line to the output.BaseTemplatepi(java.util.Map<?,?> attrs)Renders processing instructions.abstract java.lang.Objectrun()java.lang.StringstringOf(Closure cl)java.lang.StringtoString()java.lang.ObjecttryEscape(java.lang.Object contents)Escapes the string representation of the supplied object if it derives fromCharSequence, otherwise returns the object itself.java.io.WriterwriteTo(java.io.Writer out)Main method used to render a template.BaseTemplatexmlDeclaration()Renders an XML declaration header.BaseTemplateyield(java.lang.Object obj)Renders the object provided as parameter using itsObject.toString()method, The contents is rendered after being escaped for XML, enforcing valid XML output.BaseTemplateyieldUnescaped(java.lang.Object obj)Renders the object provided as parameter using itsObject.toString()method, The contents is rendered as is, unescaped.
 
- 
- 
- 
Constructor Detail- 
BaseTemplatepublic BaseTemplate(MarkupTemplateEngine templateEngine, java.util.Map model, java.util.Map<java.lang.String,java.lang.String> modelTypes, TemplateConfiguration configuration) 
 
- 
 - 
Method Detail- 
getModelpublic java.util.Map getModel() 
 - 
runpublic abstract java.lang.Object run() 
 - 
yieldUnescapedpublic BaseTemplate yieldUnescaped(java.lang.Object obj) throws java.io.IOException Renders the object provided as parameter using itsObject.toString()method, The contents is rendered as is, unescaped. This means that depending on what theObject.toString()method call returns, you might create invalid markup.- Parameters:
- obj- the object to be rendered unescaped
- Returns:
- this template instance
- Throws:
- java.io.IOException
 
 - 
yieldpublic BaseTemplate yield(java.lang.Object obj) throws java.io.IOException Renders the object provided as parameter using itsObject.toString()method, The contents is rendered after being escaped for XML, enforcing valid XML output.- Parameters:
- obj- the object to be rendered
- Returns:
- this template instance
- Throws:
- java.io.IOException
 
 - 
stringOfpublic java.lang.String stringOf(Closure cl) throws java.io.IOException - Throws:
- java.io.IOException
 
 - 
commentpublic BaseTemplate comment(java.lang.Object cs) throws java.io.IOException Renders the supplied object using itsObject.toString()method inside a comment markup block (<!-- ... -->). The object is rendered as is, unescaped.- Parameters:
- cs- the object to be rendered inside an XML comment block.
- Returns:
- this template instance.
- Throws:
- java.io.IOException
 
 - 
xmlDeclarationpublic BaseTemplate xmlDeclaration() throws java.io.IOException Renders an XML declaration header. If the declaration encoding is set in thetemplate configuration, then the encoding is rendered into the declaration.- Returns:
- this template instance
- Throws:
- java.io.IOException
 
 - 
pipublic 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'?>- Parameters:
- attrs- the attributes to render
- Returns:
- this template instance
- Throws:
- java.io.IOException
 
 - 
methodMissingpublic java.lang.Object methodMissing(java.lang.String tagName, java.lang.Object args) throws java.io.IOExceptionThis 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- the name of the tag
- args- tag generation arguments
- Returns:
- this template instance
- Throws:
- java.io.IOException
 
 - 
includeGroovypublic void includeGroovy(java.lang.String templatePath) throws java.io.IOException, java.lang.ClassNotFoundExceptionIncludes another template inside this template.- Parameters:
- templatePath- the path to the included resource.
- Throws:
- java.io.IOException
- java.lang.ClassNotFoundException
 
 - 
includeEscapedpublic void includeEscaped(java.lang.String templatePath) throws java.io.IOExceptionIncludes contents of another file, not as a template but as escaped text.- Parameters:
- templatePath- the path to the other file
- Throws:
- java.io.IOException
 
 - 
includeUnescapedpublic void includeUnescaped(java.lang.String templatePath) throws java.io.IOExceptionIncludes contents of another file, not as a template but as unescaped text.- Parameters:
- templatePath- the path to the other file
- Throws:
- java.io.IOException
 
 - 
tryEscapepublic java.lang.Object tryEscape(java.lang.Object contents) Escapes the string representation of the supplied object if it derives fromCharSequence, otherwise returns the object itself.- Parameters:
- contents- an object to be escaped for XML
- Returns:
- an escaped string, or the object itself
 
 - 
getOutpublic java.io.Writer getOut() Convenience method to return the current writer instance.- Returns:
- the current writer
 
 - 
newLinepublic void newLine() throws java.io.IOExceptionAdds a new line to the output. The new line string can be configured byTemplateConfiguration.setNewLineString(String)- Throws:
- java.io.IOException
 
 - 
fragmentpublic java.lang.Object fragment(java.util.Map model, java.lang.String templateText) throws java.io.IOException, java.lang.ClassNotFoundExceptionRenders 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 usinglayout(java.util.Map, String).- Parameters:
- model- model to be passed to the template
- templateText- template body
- Returns:
- this template instance
- Throws:
- java.io.IOException
- java.lang.ClassNotFoundException
 
 - 
layoutpublic java.lang.Object layout(java.util.Map model, java.lang.String templateName) throws java.io.IOException, java.lang.ClassNotFoundExceptionImports 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, seelayout(java.util.Map, String, boolean).- Parameters:
- model- model to be passed to the template
- templateName- the name of the template to be used as a layout
- Returns:
- this template instance
- Throws:
- java.io.IOException
- java.lang.ClassNotFoundException
 
 - 
layoutpublic java.lang.Object layout(java.util.Map model, java.lang.String templateName, boolean inheritModel) throws java.io.IOException, java.lang.ClassNotFoundExceptionImports 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- model to be passed to the template
- templateName- the name of the template to be used as a layout
- inheritModel- a boolean indicating if we should inherit the parent model
- Returns:
- this template instance
- Throws:
- java.io.IOException
- java.lang.ClassNotFoundException
 
 - 
contentspublic Closure contents(Closure 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- the fragment to be wrapped
- Returns:
- a wrapped closure returning an empty string
 
 - 
writeTopublic java.io.Writer writeTo(java.io.Writer out) throws java.io.IOExceptionMain method used to render a template.
 - 
toStringpublic java.lang.String toString() - Overrides:
- toStringin class- java.lang.Object
 
 
- 
 
-