Class SimpleTemplateEngine
- java.lang.Object
-
- groovy.text.TemplateEngine
-
- hudson.plugins.emailext.groovy.sandbox.SimpleTemplateEngine
-
public class SimpleTemplateEngine extends groovy.text.TemplateEngine
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 forTemplateServlet
by placing the following in yourweb.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.- Author:
- sam, Christian Stein, Paul King, Alex Tkachman
-
-
Field Summary
Fields Modifier and Type Field Description protected groovy.lang.GroovyShell
groovyShell
static int
MAX_EXPANDED_SIZE_BYTES
Maximum size of template expansion we expect to produce.
-
Constructor Summary
Constructors Constructor Description SimpleTemplateEngine(groovy.lang.GroovyShell groovyShell, boolean sandbox)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description groovy.text.Template
createTemplate(Reader reader)
groovy.text.Template
createTemplate(Reader reader, String fileName)
protected String
parse(Reader 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.protected groovy.lang.Script
parseScript(Reader reader, String fileName)
protected String
printMethod()
-
-
-
Method Detail
-
createTemplate
public groovy.text.Template createTemplate(Reader reader) throws org.codehaus.groovy.control.CompilationFailedException, IOException
- Specified by:
createTemplate
in classgroovy.text.TemplateEngine
- Throws:
org.codehaus.groovy.control.CompilationFailedException
IOException
-
createTemplate
public groovy.text.Template createTemplate(Reader reader, String fileName) throws org.codehaus.groovy.control.CompilationFailedException, IOException
- Throws:
org.codehaus.groovy.control.CompilationFailedException
IOException
-
parseScript
protected groovy.lang.Script parseScript(Reader reader, String fileName) throws org.codehaus.groovy.control.CompilationFailedException, IOException
- Throws:
org.codehaus.groovy.control.CompilationFailedException
IOException
-
parse
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
- a reader for the template text- Returns:
- the parsed text
- Throws:
IOException
- if something goes wrong
-
printMethod
protected String printMethod()
-
-