There is at least one way to do it. It doesn't work in your case for the same reasons you can't use -
character in variable name. When you define an expression like:
${first-name}
Groovy sees it as:
${first.minus(name)}
and that is why it throws exception like:
Caught: groovy.lang.MissingPropertyException: No such property: first for class: SimpleTemplateScript1
groovy.lang.MissingPropertyException: No such property: first for class: SimpleTemplateScript1
at SimpleTemplateScript1.run(SimpleTemplateScript1.groovy:1)
You can keep keys like first-name
in your bindings, but you would have to put them in a map, so you can access first-name
value using Map.get(key)
function. Consider following example:
import groovy.text.SimpleTemplateEngine
def engine = new SimpleTemplateEngine()
def bindings = [
'first-name': 'Test',
'lastname': 'qwe'
]
String text = 'Dear "${map["first-name"]} ${map["lastname"]}",\nSo nice to meet you'
println engine.createTemplate(text).make([map: bindings])
In this example I put bindings into a map with a key map
. When I do so I can access my bindings as map['first-name']
and map['lastname']
. Running this example produces expected output:
Dear "Test qwe",
So nice to meet you
Hope it helps.