title: Storing GStrings in a variable and evaluate at runtime
body: <p>I am storing a few GStrings in a map and am trying to evaluate them at runtime as:</p>
  
  <pre><code>//this is in begginning of my class or controller or service and is a global variable
  def placeholderStrings=["nameString":"My name is $name","professionString":"My profession is $profession"]
  
  //this params map comes after arrival of request from client side and following code is inside the controller
  params=["name":"name1","profession":"profession1"]
  def paramsKeyMap=params.keySet()
  paramsKeyMap.each{
      bindings."$it"=params[it]
      println it
  }
  println placeholderStrings.get("nameString") // this line gives error as groovy.lang.MissingPropertyException: No such property: name
  </code></pre>
  
  <p>Above code gives error but below code runs:</p>
  
  <pre><code>params=["name":"name1","profession":"profession1"]
  def keyMap=params.keySet()
  keyMap.each { 
      binding[it] =params[it] 
      println it // or even binding[it] = 'something' 
  }
  //Now I am declaring it locally pls note this
  def placeholderStrings=["nameString":"My name is $name","professionString":"My profession is $profession"]
  println placeholderStrings.get("nameString") // this line gives error as groovy.lang.MissingPropertyException: No such property: name
  </code></pre>
  
  <p>As we can see the difference, that in the first script, the placeholderStrings were declared before the bindings, while in second, it was after the bindings. I want to do something similar to first situation because in my application, I need to evaluate the placeholder strings at runtime based on client requests. </p>
  
  <ul>
  <li>When the request comes, I need to get the appropriate value using the <code>placeholderStrings.get(keyFromParams)</code>.</li>
  <li>Substitute the placeholders from bindings in above obtained string.</li>
  </ul>
  
  <p>Now the issue is that when ever I call the <code>placeholderStrings.get(keyFromParams)</code>, it tries to substitute the placeholders in the string and throws exception.</p>
  
  <p>To fix the issue I tried to store the placeholderString values as single quoted strings like:</p>
  
  <pre><code>def placeholderStrings=["nameString":'My name is $name',"professionString":'My profession is $profession']
  </code></pre>
  
  <p>But then now I have no way to convert the values in above map to GString so that the placeholders can be replaced.So can anyone suggest what can be done?</p>