Package groovy.text

# Class GStringTemplateEngine

java.lang.Object
groovy.text.TemplateEngine
groovy.text.GStringTemplateEngine

public class GStringTemplateEngine extends TemplateEngine
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.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `GStringTemplateEngine()`
  Creates an engine that parses template scripts with the default template class loader.
  `GStringTemplateEngine(ClassLoader parentLoader)`
  Creates an engine that parses template scripts with the supplied parent loader.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Template`
  `createTemplate(Reader reader)`
  Creates a template backed by writable closures generated from the supplied reader.

  ### Methods inherited from class groovy.text.TemplateEngine

  `createTemplate, createTemplate, createTemplate, createTemplate, createTemplate`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### GStringTemplateEngine

    public GStringTemplateEngine()
    Creates an engine that parses template scripts with the default template class loader.

  - ### GStringTemplateEngine

    public GStringTemplateEngine(ClassLoader parentLoader)
    Creates an engine that parses template scripts with the supplied parent loader.
    Parameters:
      `parentLoader` - class loader used to compile generated template scripts

- ## Method Details

  - ### createTemplate

    public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException
    Creates a template backed by writable closures generated from the supplied reader.
    Specified by:
      `createTemplate` in class `TemplateEngine`
    Parameters:
      `reader` - template source
    Returns:
      a compiled template instance
    Throws:
      `CompilationFailedException` - if Groovy fails to compile the generated template script
      `ClassNotFoundException` - if the generated template class cannot be instantiated
      `IOException` - if reading the template source fails