The need is very simple: resolve a string with the current execution context.
Example:
def templateToResolve = "${hello}" // This comes from a function, the variable is not resolved here
def hello = "world"
// How to "resolve" templateToResolve without knowing the existence of hello
println templateToResolve // world
Maybe we could extract the Binding of the execution context and use it with StreamingTemplateEngine but I didn't find a way to get the current binding. I also gave a look to Eval.me but Eval does not use external context. I even checked dynamic compilation without success.
The solution must not use hello var in any way, it must be dynamic.
So bottom line is using GString (ie "${hello}") you can't. There is no concept of a binding that can be defined later with GString. The variables you reference in your GString must be visible in the context where they are defined. GString internally doesn't know the expression that was used to evaluate the value it holds so the binding is lost. Now you can do fancy things with this like translating the GString into other representations like this:
But I don't know if that's what you're after. I think you might want to look at
SimpleTemplateEnginewhich would let you define the template you want without the context embedded within it. Like so:For a full discussion see the Groovy docs on Template Engines