Package: groovy.text

## [Java] Class SimpleTemplateEngine

- 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 `groovy.servlet.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 Summary

    Constructors
    Constructor and description
    `SimpleTemplateEngine()` Creates an engine that parses template scripts with the default Groovy class loader.
    `SimpleTemplateEngine(boolean verbose)` Creates an engine with optional debug output enabled.
    `SimpleTemplateEngine(ClassLoader parentLoader)` Creates an engine that uses a GroovyShell built from the supplied parent loader.
    `SimpleTemplateEngine(GroovyShell groovyShell)` Creates an engine backed by the supplied GroovyShell.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public Template` | `createTemplate(Reader reader)` Compiles template source from the supplied reader.
    `` | `public boolean` | `isEscapeBackslash()` Indicates whether backslashes are escaped while parsing template source.
    `` | `public boolean` | `isVerbose()` Indicates whether generated template scripts are printed during compilation.
    `` | `public void` | `setEscapeBackslash(boolean escapeBackslash)` Controls whether backslashes in template source should be preserved using the legacy GROOVY-4585 behavior.
    `` | `public void` | `setVerbose(boolean verbose)` Parameters: `verbose` - true if you want the engine to display the template source file for debugging purposes

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class TemplateEngine` | `createTemplate, createTemplate, createTemplate, createTemplate, createTemplate, createTemplate`

- - ### Constructor Detail

    - #### public SimpleTemplateEngine()

      Creates an engine that parses template scripts with the default Groovy class loader.

    - #### public SimpleTemplateEngine(boolean verbose)

      Creates an engine with optional debug output enabled.

      Parameters:
        `verbose` - `true` to print generated script source while compiling templates

    - #### public SimpleTemplateEngine(ClassLoader parentLoader)

      Creates an engine that uses a GroovyShell built from the supplied parent loader.

      Parameters:
        `parentLoader` - class loader used to compile generated template scripts

    - #### public SimpleTemplateEngine(GroovyShell groovyShell)

      Creates an engine backed by the supplied GroovyShell.

      Parameters:
        `groovyShell` - shell used to compile generated template scripts

  - ### Method Detail

    - #### public Template createTemplate(Reader reader)

      Compiles template source from the supplied reader.

      throws:
        CompilationFailedException if Groovy fails to compile the generated template script
      throws:
        IOException if reading the template source fails
      Parameters:
        `reader` - template source
      Returns:
        a compiled template instance

    - #### public boolean isEscapeBackslash()

      Indicates whether backslashes are escaped while parsing template source.

      Returns:
        `true` if backslash escaping is enabled

    - #### public boolean isVerbose()

      Indicates whether generated template scripts are printed during compilation.

      Returns:
        `true` if debug output is enabled

    - #### public void setEscapeBackslash(boolean escapeBackslash)

      Controls whether backslashes in template source should be preserved using the legacy GROOVY-4585 behavior.

      Parameters:
        `escapeBackslash` - `true` to escape backslashes while parsing the template

    - #### public void setVerbose(boolean verbose)

      Parameters:
        `verbose` - true if you want the engine to display the template source file for debugging purposes