SimpleTemplateEngine but creates the template using writable
closures making it potentially more scalable for large templates or in streaming scenarios.
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.GStringTemplateEngine()
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>GStringTemplate</servlet-name>
<servlet-class>groovy.servlet.TemplateServlet</servlet-class>
<init-param>
<param-name>template.engine</param-name>
<param-value>groovy.text.GStringTemplateEngine</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 final ClassLoader | |
| private static final boolean |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Template | createTemplate(final Reader reader)
Implements abstract groovy. Creates a template by reading content from the Reader. |
| counter | back to summary |
|---|---|
| private static AtomicInteger counter | |
| parentLoader | back to summary |
|---|---|
| private final ClassLoader parentLoader | |
| reuseClassLoader | back to summary |
|---|---|
| private static final boolean reuseClassLoader | |
| GStringTemplateEngine | back to summary |
|---|---|
| public GStringTemplateEngine() | |
| GStringTemplateEngine | back to summary |
|---|---|
| public GStringTemplateEngine(ClassLoader parentLoader) | |
| createTemplate | back to summary |
|---|---|
| public Template createTemplate(final Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException Implements abstract groovy. Doc from groovy. Creates a template by reading content from the Reader. | |
| Access | Constructor and Description |
|---|---|
| pack-priv | GStringTemplate(final Reader reader, final ClassLoader parentLoader)
Turn the template into a writable Closure When executed the closure evaluates all the code embedded in the template and then writes a GString containing the fixed and variable items to the writer passed as a parameter For example:
'<%= "test" %> of expr and <% test = 1 %>${test} script.'
|
| Modifier and Type | Method and Description |
|---|---|
| private static void | |
| public Writable | |
| public Writable | |
| private static void | parseExpression(final Reader reader, final boolean writingString, final StringBuilder templateExpressions)
Parse a <%= .... |
| private void | |
| private static void | parseSection(final int pendingC, final Reader reader, final boolean writingString, final StringBuilder templateExpressions)
Parse a <% .... |
| private static void |
| template | back to summary |
|---|---|
| pack-priv final Closure<V> template | |
| GStringTemplate | back to summary |
|---|---|
| pack-priv GStringTemplate(final Reader reader, final ClassLoader parentLoader) throws CompilationFailedException, ClassNotFoundException, IOException Turn the template into a writable Closure When executed the closure evaluates all the code embedded in the template and then writes a GString containing the fixed and variable items to the writer passed as a parameter For example:
'<%= "test" %> of expr and <% test = 1 %>${test} script.'
would compile into:
{ out -> out << "${"test"} of expr and "; test = 1 ; out << "${test} script."}.asWritable()
| |
| appendCharacter | back to summary |
|---|---|
| private static void appendCharacter(final char c, final StringBuilder templateExpressions, final boolean writingString) | |
| make | back to summary |
|---|---|
| public Writable make() Implements groovy. | |
| make | back to summary |
|---|---|
| public Writable make(final Map<K, V> map) Implements groovy. | |
| parseExpression | back to summary |
|---|---|
| private static void parseExpression(final Reader reader, final boolean writingString, final StringBuilder templateExpressions) throws IOException Parse a <%= .... %> expression | |
| parseGSstring | back to summary |
|---|---|
| private void parseGSstring(Reader reader, boolean writingString, StringBuilder templateExpressions) throws IOException | |
| parseSection | back to summary |
|---|---|
| private static void parseSection(final int pendingC, final Reader reader, final boolean writingString, final StringBuilder templateExpressions) throws IOException Parse a <% .... %> section if we are writing a GString close and append ';' then write the section as a statement | |
| readAndAppend | back to summary |
|---|---|
| private static void readAndAppend(Reader reader, StringBuilder templateExpressions) throws IOException | |