Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this introductory tutorial, we’ll explore the concept of template engines in Groovy.

In Groovy, we can use GStrings to generate dynamic text easily. However, the template engines provide a better way of handling dynamic text using static templates.

These templates are convenient in defining static templates for various notifications like SMS and emails.

2. What Is Groovy’s TemplateEngine?

Groovy’s TemplateEngine is an abstract class that contains the createTemplate method.

All template framework engines available in Groovy extend TemplateEngine and implement createTemplate. Additionally, every engine returns the Template interface object.

The Template interface has a method make, which takes a map for binding variables. Therefore, it must be implemented by every template framework.

Let’s discuss the functionality and behavior of all the available template frameworks in Groovy.

3. SimpleTemplateEngine

The SimpleTemplateEngine generates dynamic text using String interpolation and scriptlets. This engine is quite useful for simple notifications like SMS and simple text emails.

For example:

def "testSimpleTemplateEngine"() {
    given:
    def smsTemplate = 'Dear <% print user %>, Thanks for reading our Article. ${signature}'

    when:
    def smsText = new SimpleTemplateEngine().createTemplate(smsTemplate).make(BIND_MAP)

    then:
    smsText.toString() == "Dear Norman, Thanks for reading our Article. Baeldung"
}

4. StreamingTemplateEngine

In a general sense, the StreamingTemplateEngine works similarly to SimpleTemplateEngine. However, internally it uses Writable closures to generate a template.

For the same reason, it holds benefits when working over larger Strings(> 64K). Hence, it’s more efficient than SimpleTemplateEngine.

Let’s write a quick example to generate a dynamic email content using a static template.

Firstly, we’ll create a static articleEmail template:

Dear <% out << (user) %>,
Please read the requested article below.
<% out << (articleText) %>
From,
<% out << (signature) %>

Here, we’re using <% %> scriptlets for dynamic text and out for the writer.

Now, we’ll generate the content of an email using StreamingTemplateEngine:

def articleEmailTemplate = new File('src/main/resources/articleEmail.template')
def bindMap = [user: "Norman", signature: "Baeldung"]

bindMap.articleText = """1. Overview
This is a tutorial article on Template Engines...""" //can be a string larger than 64k

def articleEmailText = new StreamingTemplateEngine().createTemplate(articleEmailTemplate).make(bindMap)

assert articleEmailText.toString() == """Dear Norman,
Please read the requested article below.
1. Overview
This is a tutorial article on Template Engines...
From,
Baeldung"""

5. GStringTemplateEngine

As the name suggests, GStringTemplateEngine uses GString to generate dynamic text from static templates.

Firstly, let’s write a simple email template using GString:

Dear $user,
Thanks for subscribing our services.
${signature}

Now, we’ll use GStringTemplateEngine to create dynamic content:

def emailTemplate = new File('src/main/resources/email.template')
def emailText = new GStringTemplateEngine().createTemplate(emailTemplate).make(bindMap)

6. XmlTemplateEngine

The XmlTemplateEngine is useful when we want to create dynamic XML outputs. It requires XML schema as input and allows two special tags, <gsp:scriptlet> to inject script and <gsp:expression> to inject an expression.

For example, let’s convert the already discussed email template to XML:

def emailXmlTemplate = '''
<xs xmlns:gsp='groovy-server-pages'>
    <gsp:scriptlet>def emailContent = "Thanks for subscribing our services."</gsp:scriptlet>
    <email>
        <greet>Dear ${user}</greet>
        <content><gsp:expression>emailContent</gsp:expression></content>
        <signature>${signature}</signature>
    </email>
</xs>'''

def emailXml = new XmlTemplateEngine().createTemplate(emailXmlTemplate).make(bindMap)

Hence, the emailXml will have XML rendered, and the content will be:

<xs>
  <email>
    <greet>
      Dear Norman
    </greet>
    <content>
      Thanks for subscribing our services.
    </content>
    <signature>
      Baeldung
    </signature>
  </email>
</xs>

It’s interesting to note the XML output is indented and beautified itself by the template framework.

7. MarkupTemplateEngine

This template framework is a complete package to generate HTML and other markup languages.

Additionally, it uses Domain Specific Language to process the templates and is the most optimized among all template frameworks available in Groovy.

7.1. HTML

Let’s write a quick example to render HTML for the already discussed email template:

def emailHtmlTemplate = """
html {
    head {
        title('Service Subscription Email')
    }
    body {
        p('Dear Norman')
        p('Thanks for subscribing our services.')
        p('Baeldung')
    }
}"""
def emailHtml = new MarkupTemplateEngine().createTemplate(emailHtmlTemplate).make()

Therefore, the content of emailHtml will be:

<html><head><title>Service Subscription Email</title></head>
<body><p>Dear Norman</p><p>Thanks for subscribing our services.</p><p>Baeldung</p></body></html>

7.2. XML

Likewise, we can render XML:

def emailXmlTemplate = """
xmlDeclaration()  
    xs{
        email {
            greet('Dear Norman')
            content('Thanks for subscribing our services.')
            signature('Baeldung')
        }  
    }"""
def emailXml = new MarkupTemplateEngine().createTemplate(emailXmlTemplate).make()

Therefore, the content of emailXml will be:

<?xml version='1.0'?>
<xs><email><greet>Dear Norman</greet><content>Thanks for subscribing our services.</content>
<signature>Baeldung</signature></email></xs>

7.3. TemplateConfiguration

Note that unlike like XmlTemplateEngine, the template output of this framework is not indented and beautified by itself.

For such configuration, we’ll use the TemplateConfiguration class:

TemplateConfiguration config = new TemplateConfiguration()
config.autoIndent = true
config.autoEscape = true
config.autoNewLine = true
                               
def templateEngine = new MarkupTemplateEngine(config)

7.4. Internationalization

Additionally, the locale property of TemplateConfiguration is available to enable the support of internationalization.

Firstly, we’ll create a static template file email.tpl and copy the already discussed emailHtmlTemplate string into it. This will be treated as the default template.

Likewise, we’ll create locale-based template files like email_ja_JP.tpl for Japanese, email_fr_FR.tpl for French, etc.

Finally, all we need is to set the locale in the TemplateConfiguration object:

config.locale = Locale.JAPAN

Hence, the corresponding locale-based template will be picked.

8. Conclusion

In this article, we’ve seen various template frameworks available in Groovy.

We can leverage these handy template engines to generate dynamic text using static templates. Therefore, they can be helpful in the dynamic generation of various kinds of notifications or on-screen messages and errors.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW