Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6037633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:06:11+00:00 2026-05-23T06:06:11+00:00

I have the following variables defined: def VAL1 = ‘foo’ def VAL2 = ‘bar’

  • 0

I have the following variables defined:

def VAL1 = 'foo'
def VAL2 = 'bar'

def s2 = 'hello ${VAL1}, please have a ${VAL2}'

What is the easiest way to make this substitution work?
How could I build a GString from s2 and have it evaluated?
(VALs and s2 are loaded from database, this snippet is only for demonstrating my problem.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:06:11+00:00Added an answer on May 23, 2026 at 6:06 am

    You can use the SimpleTemplateEngine if you can get your variables into a Map?

    import groovy.text.SimpleTemplateEngine
    
    def binding = [ VAL1:'foo', VAL2:'bar' ]
    
    def template = 'hello ${VAL1}, please have a ${VAL2}'
    
    println new SimpleTemplateEngine().createTemplate( template ).make( binding ).toString()
    

    edit

    You can use the binding instead of the map, so the following works in the groovyconsole:

    // No def.  We want the vars in the script's binding
    VAL1 = 'foo'
    VAL2 = 'bar'
    
    def template = 'hello ${VAL1}, please have a ${VAL2}'
    
    // Pass the variables defined in the binding to the Template
    new SimpleTemplateEngine().createTemplate( template ).make( binding.variables ).toString()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the following code: class Foo(object): bar = 1 def bah(self): print(bar)
I have the following Ruby code: local_var = Hello def hello puts local_var end
Let's say I have the following variables defined: a = 6 b = 4
Let's assume I have the following file structure: data.py foo = [] bar =
I have the following 2 tables defined in migrations class CreateUsers < ActiveRecord::Migration def
i have following variables in jquery var uemail=abc@domain.com,xyz@domain.com,gty@domain.com; var subj=test message; var text=<b>This is
I have the following lines in my ~/.emacs.d/init.el (custom-set-variables '(flymake-allowed-file-name-masks (quote ( (\\.cc\\' flymake-simple-make-init)
I have the following use case, a struct with some boolean and int variables
So I have a class that has the following member variables. I have get
I have text documents like the following which contain single and multiple variables: title::

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.