Nov 11, 2014

StringTemplates in Groovy

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:

“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:

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!

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:

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

About the Author

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

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.

One thought on “StringTemplates in Groovy

  1. Sagar says:

    Nice Article..

  2. jnorthr says:

    Simply beautiful 🙂

Leave a Reply to Sagar Cancel reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
TICK Stack Monitoring for the Non-Technical
TICK – Telegraf, Influx, Chronograf, and Kapacitor – is a method of monitoring your systems and applications. In this article, I discuss in non-technical terms what the difference is between TICK and Prometheus Grafana A […]
Design Systems, Part 1 • Introduction
Business leaders need a practical guide to plan and execute Design System Initiatives. The aim of this series is to be that guide. This installment introduces terms and definitions as a primer on Design Systems.
ML for Translating Dysarthria Speech (Pre-Part 1)
What is Dysarthria? Per the Mayo Clinic, Dysarthria occurs when the muscles you use for speech are weak or you have difficulty controlling them. Dysarthria often causes slurred or slow speech that can be difficult […]
Develop Quality Code
As software continues to dominate every facet of our lives, developers are faced with an ever-increasing pressure to produce bug free code. The responsibility of clean quality software falls upon everyone that is involved in […]