Top Description Inners Fields Constructors Methods
groovy.text

public Class GStringTemplateEngine

extends TemplateEngine
Class Inheritance
Imports
groovy.lang.Binding, .Closure, .GroovyClassLoader, .GroovyCodeSource, .GroovyObject, .GroovyRuntimeException, .Writable, org.apache.groovy.util.SystemUtil, org.codehaus.groovy.control.CompilationFailedException, java.io.IOException, .Reader, java.lang.reflect.InvocationTargetException, java.security.AccessController, .PrivilegedAction, 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 using a streaming approach. This engine has equivalent functionality to the 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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static AtomicInteger
private final ClassLoader
private static final boolean

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

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

Implements abstract groovy.text.TemplateEngine.createTemplate.

Creates a template by reading content from the Reader.

Inherited from groovy.text.TemplateEngine:
createTemplatecreateTemplatecreateTemplatecreateTemplatecreateTemplate

Field Detail

counterback to summary
private static AtomicInteger counter
parentLoaderback to summary
private final ClassLoader parentLoader
reuseClassLoaderback to summary
private static final boolean reuseClassLoader

Constructor Detail

GStringTemplateEngineback to summary
public GStringTemplateEngine()
GStringTemplateEngineback to summary
public GStringTemplateEngine(ClassLoader parentLoader)

Method Detail

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

Implements abstract groovy.text.TemplateEngine.createTemplate.

Doc from groovy.text.TemplateEngine.createTemplate.

Creates a template by reading content from the Reader.

groovy.text back to summary

private Class GStringTemplateEngine.GStringTemplate

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

Field Summary

Modifier and TypeField and Description
pack-priv final Closure<V>

Constructor Summary

AccessConstructor 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.'

Method Summary

Modifier and TypeMethod and Description
private static void
appendCharacter(final char c, final StringBuilder templateExpressions, final boolean writingString)

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

Implements groovy.text.Template.make.

private static void
parseExpression(final Reader reader, final boolean writingString, final StringBuilder templateExpressions)

Parse a <%= ....

private void
parseGSstring(Reader reader, boolean writingString, StringBuilder templateExpressions)

private static void
parseSection(final int pendingC, final Reader reader, final boolean writingString, final StringBuilder templateExpressions)

Parse a <% ....

private static void
readAndAppend(Reader reader, StringBuilder templateExpressions)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

templateback to summary
pack-priv final Closure<V> template

Constructor Detail

GStringTemplateback 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()

Method Detail

appendCharacterback to summary
private static void appendCharacter(final char c, final StringBuilder templateExpressions, final boolean writingString)
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.

parseExpressionback to summary
private static void parseExpression(final Reader reader, final boolean writingString, final StringBuilder templateExpressions) throws IOException

Parse a <%= .... %> expression

parseGSstringback to summary
private void parseGSstring(Reader reader, boolean writingString, StringBuilder templateExpressions) throws IOException
parseSectionback 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

readAndAppendback to summary
private static void readAndAppend(Reader reader, StringBuilder templateExpressions) throws IOException