Top Description Inners Fields Constructors Methods
groovy.text

public Class SimpleTemplateEngine

extends TemplateEngine
Class Inheritance
Imports
groovy.lang.Binding, .GroovyRuntimeException, .GroovyShell, .Script, .Writable, org.apache.groovy.io.StringBuilderWriter, org.codehaus.groovy.control.CompilationFailedException, org.codehaus.groovy.runtime.InvokerHelper, java.io.BufferedReader, .IOException, .PrintWriter, .Reader, .Writer, java.util.Map, java.util.concurrent.atomic.AtomicInteger

Processes template source files substituting variables and expressions into placeholders in a template source text to produce the desired output.

The template engine uses JSP style <% %> script and <%= %> expression syntax or GString style expressions. The variable 'out' is bound to the writer that the template is being written to.

Frequently, the template source will be in a file but here is a simple example providing the template as a string:

def binding = [
    firstname : "Grace",
    lastname  : "Hopper",
    accepted  : true,
    title     : 'Groovy for COBOL programmers'
]
def engine = new groovy.text.SimpleTemplateEngine()
def text = '''\
Dear <%= firstname %> $lastname,

We <% if (accepted) print 'are pleased' else print 'regret' %> \
to inform you that your paper entitled
'$title' was ${ accepted ? 'accepted' : 'rejected' }.

The conference committee.
'''
def template = engine.createTemplate(text).make(binding)
println template.toString()
This example uses a mix of the JSP style and GString style placeholders but you can typically use just one style if you wish. Running this example will produce this output:
Dear Grace Hopper,

We are pleased to inform you that your paper entitled
'Groovy for COBOL programmers' was accepted.

The conference committee.
The template engine can also be used as the engine for groovy.servlet.TemplateServlet by placing the following in your web.xml file (plus a corresponding servlet-mapping element):
<servlet>
  <servlet-name>SimpleTemplate</servlet-name>
  <servlet-class>groovy.servlet.TemplateServlet</servlet-class>
  <init-param>
    <param-name>template.engine</param-name>
    <param-value>groovy.text.SimpleTemplateEngine</param-value>
  </init-param>
</servlet>
In this case, your template source file should be HTML with the appropriate embedded placeholders.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static AtomicInteger
private boolean
private GroovyShell
private boolean

Constructor Summary

AccessConstructor and Description
public
public
SimpleTemplateEngine(boolean verbose)

public
public

Method Summary

Modifier and TypeMethod and Description
public Template
createTemplate(Reader reader)

Implements abstract groovy.text.TemplateEngine.createTemplate.

Creates a template by reading content from the Reader.

public boolean
public boolean
public void
setEscapeBackslash(boolean escapeBackslash)

public void
setVerbose(boolean
true if you want the engine to display the template source file for debugging purposes
verbose
)

Inherited from groovy.text.TemplateEngine:
createTemplatecreateTemplatecreateTemplatecreateTemplatecreateTemplate

Field Detail

counterback to summary
private static AtomicInteger counter
escapeBackslashback to summary
private boolean escapeBackslash
groovyShellback to summary
private GroovyShell groovyShell
verboseback to summary
private boolean verbose

Constructor Detail

SimpleTemplateEngineback to summary
public SimpleTemplateEngine()
SimpleTemplateEngineback to summary
public SimpleTemplateEngine(boolean verbose)
SimpleTemplateEngineback to summary
public SimpleTemplateEngine(ClassLoader parentLoader)
SimpleTemplateEngineback to summary
public SimpleTemplateEngine(GroovyShell groovyShell)

Method Detail

createTemplateback to summary
public Template createTemplate(Reader reader) throws CompilationFailedException, IOException

Implements abstract groovy.text.TemplateEngine.createTemplate.

Doc from groovy.text.TemplateEngine.createTemplate.

Creates a template by reading content from the Reader.

isEscapeBackslashback to summary
public boolean isEscapeBackslash()
isVerboseback to summary
public boolean isVerbose()
setEscapeBackslashback to summary
public void setEscapeBackslash(boolean escapeBackslash)
setVerboseback to summary
public void setVerbose(boolean verbose)
Parameters
verbose:boolean

true if you want the engine to display the template source file for debugging purposes

groovy.text back to summary

private Class SimpleTemplateEngine.SimpleTemplate

extends Object
implements Template
Class Inheritance
All Implemented Interfaces
groovy.text.Template

Field Summary

Modifier and TypeField and Description
private boolean
protected Script

Constructor Summary

AccessConstructor and Description
public
public
SimpleTemplate(boolean escapeBackslash)

Method Summary

Modifier and TypeMethod and Description
private void
private void
groovyExpression(Reader
a reader for the template text
reader
,
StringBuilderWriter
a StringBuilderWriter to write expression content
sw
)

Closes the currently open write and writes out the following text as a GString expression until it reaches an end %>.

private void
groovySection(Reader
a reader for the template text
reader
,
StringBuilderWriter
a StringBuilderWriter to write expression content
sw
)

Closes the currently open write and writes the following text as normal Groovy script code until it reaches an end %>.

public Writable
public Writable
make(final Map<K, V> map)

Implements groovy.text.Template.make.

protected String

Returns:

the parsed text
parse
(Reader
a reader for the template text
reader
)

Parse the text document looking for <% or <%= and then call out to the appropriate handler, otherwise copy the text directly into the script while escaping quotes.

private void
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

escapeBackslashback to summary
private boolean escapeBackslash
scriptback to summary
protected Script script

Constructor Detail

SimpleTemplateback to summary
public SimpleTemplate()
SimpleTemplateback to summary
public SimpleTemplate(boolean escapeBackslash)

Method Detail

endScriptback to summary
private void endScript(StringBuilderWriter sw)
groovyExpressionback to summary
private void groovyExpression(Reader reader, StringBuilderWriter sw) throws IOException

Closes the currently open write and writes out the following text as a GString expression until it reaches an end %>.

Parameters
reader:Reader

a reader for the template text

sw:StringBuilderWriter

a StringBuilderWriter to write expression content

Exceptions
IOException:
if something goes wrong
groovySectionback to summary
private void groovySection(Reader reader, StringBuilderWriter sw) throws IOException

Closes the currently open write and writes the following text as normal Groovy script code until it reaches an end %>.

Parameters
reader:Reader

a reader for the template text

sw:StringBuilderWriter

a StringBuilderWriter to write expression content

Exceptions
IOException:
if something goes wrong
makeback to summary
public Writable make()

Implements groovy.text.Template.make.

makeback to summary
public Writable make(final Map<K, V> map)

Implements groovy.text.Template.make.

parseback to summary
protected String parse(Reader reader) throws IOException

Parse the text document looking for <% or <%= and then call out to the appropriate handler, otherwise copy the text directly into the script while escaping quotes.

Parameters
reader:Reader

a reader for the template text

Returns:String

the parsed text

Exceptions
IOException:
if something goes wrong
processGSstringback to summary
private void processGSstring(Reader reader, StringBuilderWriter sw) throws IOException
startScriptback to summary
private void startScript(StringBuilderWriter sw)