Package groovy.text
Class SimpleTemplateEngine
java.lang.Object
groovy.text.TemplateEngine
groovy.text.SimpleTemplateEngine
public class SimpleTemplateEngine extends 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 for
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.- 
Constructor SummaryConstructors Constructor Description SimpleTemplateEngine()SimpleTemplateEngine(boolean verbose)SimpleTemplateEngine(GroovyShell groovyShell)SimpleTemplateEngine(java.lang.ClassLoader parentLoader)
- 
Method SummaryModifier and Type Method Description TemplatecreateTemplate(java.io.Reader reader)Creates a template by reading content from the Reader.booleanisEscapeBackslash()booleanisVerbose()voidsetEscapeBackslash(boolean escapeBackslash)voidsetVerbose(boolean verbose)Methods inherited from class groovy.text.TemplateEnginecreateTemplate, createTemplate, createTemplate, createTemplate, createTemplateMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
- 
Constructor Details- 
SimpleTemplateEnginepublic SimpleTemplateEngine()
- 
SimpleTemplateEnginepublic SimpleTemplateEngine(boolean verbose)
- 
SimpleTemplateEnginepublic SimpleTemplateEngine(java.lang.ClassLoader parentLoader)
- 
SimpleTemplateEngine
 
- 
- 
Method Details- 
createTemplatepublic Template createTemplate(java.io.Reader reader) throws CompilationFailedException, java.io.IOExceptionDescription copied from class:TemplateEngineCreates a template by reading content from the Reader.- Specified by:
- createTemplatein class- TemplateEngine
- Throws:
- CompilationFailedException
- java.io.IOException
 
- 
setVerbosepublic void setVerbose(boolean verbose)- Parameters:
- verbose- true if you want the engine to display the template source file for debugging purposes
 
- 
isVerbosepublic boolean isVerbose()
- 
isEscapeBackslashpublic boolean isEscapeBackslash()
- 
setEscapeBackslashpublic void setEscapeBackslash(boolean escapeBackslash)
 
-