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.
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private static AtomicInteger | |
| private boolean | |
| private GroovyShell | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Template | createTemplate(Reader reader)
Implements abstract groovy. Creates a template by reading content from the Reader. |
| public boolean | |
| public boolean | |
| public void | |
| public void | setVerbose(boolean
true if you want the engine to display the template source file for debugging purposes verbose)
|
| counter | back to summary |
|---|---|
| private static AtomicInteger counter | |
| escapeBackslash | back to summary |
|---|---|
| private boolean escapeBackslash | |
| groovyShell | back to summary |
|---|---|
| private GroovyShell groovyShell | |
| verbose | back to summary |
|---|---|
| private boolean verbose | |
| SimpleTemplateEngine | back to summary |
|---|---|
| public SimpleTemplateEngine() | |
| SimpleTemplateEngine | back to summary |
|---|---|
| public SimpleTemplateEngine(boolean verbose) | |
| SimpleTemplateEngine | back to summary |
|---|---|
| public SimpleTemplateEngine(ClassLoader parentLoader) | |
| SimpleTemplateEngine | back to summary |
|---|---|
| public SimpleTemplateEngine(GroovyShell groovyShell) | |
| createTemplate | back to summary |
|---|---|
| public Template createTemplate(Reader reader) throws CompilationFailedException, IOException Implements abstract groovy. Doc from groovy. Creates a template by reading content from the Reader. | |
| isEscapeBackslash | back to summary |
|---|---|
| public boolean isEscapeBackslash() | |
| isVerbose | back to summary |
|---|---|
| public boolean isVerbose() | |
| setEscapeBackslash | back to summary |
|---|---|
| public void setEscapeBackslash(boolean escapeBackslash) | |
| setVerbose | back to summary |
|---|---|
public void setVerbose(boolean verbose)
| |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| protected Script |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method 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 | |
| protected String | |
| private void | |
| private void |
| escapeBackslash | back to summary |
|---|---|
| private boolean escapeBackslash | |
| script | back to summary |
|---|---|
| protected Script script | |
| SimpleTemplate | back to summary |
|---|---|
| public SimpleTemplate() | |
| SimpleTemplate | back to summary |
|---|---|
| public SimpleTemplate(boolean escapeBackslash) | |
| endScript | back to summary |
|---|---|
| private void endScript(StringBuilderWriter sw) | |
| groovyExpression | back 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 %>.
| |
| groovySection | back 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 %>.
| |
| make | back to summary |
|---|---|
| public Writable make() Implements groovy. | |
| make | back to summary |
|---|---|
| public Writable make(final Map<K, V> map) Implements groovy. | |
| parse | back 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.
| |
| processGSstring | back to summary |
|---|---|
| private void processGSstring(Reader reader, StringBuilderWriter sw) throws IOException | |
| startScript | back to summary |
|---|---|
| private void startScript(StringBuilderWriter sw) | |