public class StreamingTemplateEngine extends TemplateEngine
SimpleTemplateEngine but creates the template using
writable closures making it more scalable for large templates.
Specifically this template engine can handle strings larger than 64k which still causes problems for the other groovy template engines.
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.
def binding = [
firstname : "Grace",
lastname : "Hopper",
accepted : false,
title : 'Groovy for COBOL programmers'
]
def text = '''\
Dear <% out.print firstname %> ${lastname},
We <% if (accepted) out.print 'are pleased' else out.print 'regret' %> \
to inform you that your paper entitled
'$title' was ${ accepted ? 'accepted' : 'rejected' }.
The conference committee.
'''
def template = new groovy.text.StreamingTemplateEngine().createTemplate(text)
print template.make(binding)
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.
TemplateServlet by placing the following in your
web.xml file (plus a corresponding servlet-mapping element):
<servlet>
<servlet-name>StreamingTemplate</servlet-name>
<servlet-class>groovy.servlet.TemplateServlet</servlet-class>
<init-param>
<param-name>template.engine</param-name>
<param-value>groovy.text.StreamingTemplateEngine</param-value>
</init-param>
</servlet>
In this case, your template source file should be HTML with the
appropriate embedded placeholders.
The template engine makes an effort to throw descriptive exceptions with context lines, ie:
groovy.text.TemplateExecutionException: Template parse error at line 4:
3: We <% if (accepted) out.print 'are pleased' else out.print 'regret' %> to inform you that your paper entitled
--> 4: '$txitle' was ${ accepted ? 'accepted' : 'rejected' }.
5:
at test.run(test.groovy:18)
Caused by: groovy.lang.MissingPropertyException: No such property: txitle for class: groovy.tmp.templates.StreamingTemplateScript1
... 1 more
and sanitize the exceptions to make things readable.
When the exceptions are not enough, it might sometimes be useful to view the actual script source generated by the template engine. This would conceptually be equivalent to viewing the .java file generated for a jsp page. The source is not currently very readable and until we get a built in groovy code pretty printer, we will probably continue to opt for compactness rather than readability.
With that being said, viewing the source might still have some value. For this reason the script source is accessible via the template.scriptSource property, i.e.:
prinltn template.scriptSource
In the above example.
| Constructor and Description |
|---|
StreamingTemplateEngine() |
StreamingTemplateEngine(ClassLoader parentLoader) |
| Modifier and Type | Method and Description |
|---|---|
Template |
createTemplate(Reader reader) |
createTemplate, createTemplate, createTemplatepublic StreamingTemplateEngine()
public StreamingTemplateEngine(ClassLoader parentLoader)
public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException
createTemplate in class TemplateEngineCompilationFailedExceptionClassNotFoundExceptionIOException