StringTemplates in Groovy

Using the StringTemplateEngine in Groovy -- troubleshooting, gotchas, and everything else..

Mike Hostetler

em { font-style: oblique; } code { font-family: monospace; }

I was looking for some some string templating in Groovy a while back and found that Groovy supports it nicely, including something called SimpleTemplateEngine, which seemed exactly what I needed. But I soon sat scratching my head, trying to figure out what was happening. Even Mr. Haki didn’t help me. It seemed that everyone’s explanation was too, well, simple. This was my initial attempt:

import groovy.text.SimpleTemplateEngine

/* this causes
 * groovy.lang.MissingPropertyException: No such property: firstName for class: bad_template
*/
def text = "${firstName} ${lastName} is a member of the ${lodge}"

def binding = [firstName: "Fred", lastName: "Flintstone",   lodge:"Water Buffaloes"]

def engine = new SimpleTemplateEngine()
template = engine.createTemplate(text).make(binding)


assert template.toString()== "Fred Flintstone is a member of the Water Buffaloes"

“Huh?” I said as I saw the strange exception pop up. The error didn’t help me figure out what I did wrong. I went back and forth between my code and the examples several times before the light-bulb went off. You have to use single-quoted strings (Java Strings) instead of double-quoted strings (GStrings). All the examples have that, but no one pointed that out specifically for unobservant people like me.

So then I made a working version:

import groovy.text.SimpleTemplateEngine

def text = '${firstName} ${lastName} is a member of the ${lodge}'

def binding = [firstName: "Fred", lastName: "Flintstone",   lodge:"Water Buffaloes"]

def engine = new SimpleTemplateEngine()
template = engine.createTemplate(text).make(binding)


assert template.toString()=="Fred Flintstone is a member of the Water Buffaloes"

That worked nicely. Of course, I don’t usually work in Maps but in my own objects. So I tried my first instinct which was the properties method. And, lo, and behold, it worked!

import groovy.text.SimpleTemplateEngine

class Member{
   String firstName
   String lastName
   String lodge
}

def text = '${firstName} ${lastName} is a member of the ${lodge}'
def member = new Member(firstName: "Fred", lastName: "Flintstone",   lodge:"Water Buffaloes")

def engine = new SimpleTemplateEngine()
// properties on a POGO returns a hash of all it's properties
template = engine.createTemplate(text).make(member.properties)


assert template.toString()=="Fred Flintstone is a member of the Water Buffaloes"

Naturally, I wasn’t going to go through the work of defining an engine and template to just use once. Again this is not something anyone shows but it’s pretty simple:

import groovy.text.SimpleTemplateEngine

class Member{
   String firstName
   String lastName
   String lodge
}

def text = '${firstName} ${lastName} is a member of the ${lodge}'

def members = [
      new Member(firstName: "Fred", lastName: "Flintstone",   lodge:"Water Buffaloes"),
      new Member(firstName: "Ralph",lastName: "Kramden", lodge: "Raccoons"),
      new Member(firstName: "Red",lastName: "Green", lodge: "Possums")
]

def engine = new SimpleTemplateEngine()
def template = engine.createTemplate(text)

members.each {
   println template.make(it.properties)
}

And there you go — more information that you probably wanted to know about Groovy’s SimpleStringTemplate

Share this Post

Related Blog Posts

JVM

Experiences with Publishing a Grails Plugin

October 29th, 2014

Blog on creating a Grails plugin to implement HTTP compression.

Patrick Double
JVM

Logging REST Exceptions with Spring

October 21st, 2014

Logging REST Exceptions with Spring using ResponseEntityExceptionHandler

Jeff Sheets
JVM

GR8Conf US Recap: Why Your Company Should Adopt Groovy!

August 25th, 2014

At the 2014 GR8Conf US, Scott Hickey from Object Partners and Jim McGill from Mutual of Omaha spoke about a large mission-critical Groovy application.

Object Partners

About the author

Mike Hostetler

Sr. Consultant

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.