Templates

Also: XmlTemplateEngine, the template engines in Grails (GSP), Micronaut views, and Thymeleaf/Freemarker from Groovy.

import groovy.text.*

def engine = new SimpleTemplateEngine()
def tpl = engine.createTemplate('''Dear ${name},
<% items.each { item -> %>  - ${item}
<% } %>Total: ${items.size()} item(s)''')
println tpl.make([name: "Ana", items: ["Book", "Pen"]])

def gstr = new GStringTemplateEngine().createTemplate('Hello ${user.toUpperCase()}')
println gstr.make(user: "bo")

def stream = new StreamingTemplateEngine().createTemplate('Large: ${(1..3).join(",")}')
println stream.make([:])

// MarkupTemplateEngine: type-checkable, compiled, fast HTML/XML templates
import groovy.text.markup.*
def config = new TemplateConfiguration(autoIndent: true, autoNewLine: true)
def mte = new MarkupTemplateEngine(config)
def page = mte.createTemplate('''
html {
    body {
        h1(title)
        ul { items.each { li(it) } }
    }
}''')
println page.make(title: "List", items: ['a', 'b'])