Here_2_learn
Here_2_learn

Reputation: 5451

Groovy : multiple loops in template engine using GStringTemplateEngine()

This question is in continuation with earlier question I have posted before.

To enhance my templates further I need to have multiple loops in the template engines and the values need to be replaced from the HashMap Variables that were derived from a configuration file.

My HashMap looks something like this, let say envVar:

envVar = [
         PROJECT:name, 
         env:[
             [name:param1, value:value1], 
             [name:param2, value:value2], 
             [name:param3, value:value3]], 
         ports:[
             [protocol:port1, port:1000], 
             [protocol:port2, port:2000]]
         ]

My template engine looks like this:

   - env:
        << NEED TO LOOP HERE WITH env variables 
      - name : param1
        value : value1 
        ....
        >> 
      project: "${PROJECT}"
      ......
      ......
      ......
      ports:
        << NEED TO LOOP HERE WITH ports variables 
      - port: 1000
        protocol : port1 
        ....
        >> 

Code snippet is described below.

    def f = new File('output.template')
    def engine = new groovy.text.GStringTemplateEngine()

    //....
    //envVar are derived from another file
    //....

    def Template = engine.createTemplate(f).make(envVar)

    println "${Template}"

Can someone explain me how to modify the above code snippet and templates to make sure the envVar will be replaced properly in the template engine.

Upvotes: 0

Views: 1315

Answers (2)

Edumelzer
Edumelzer

Reputation: 1086

You need to do an each for every variable. Your template file needs to look like this:

#cat output.template
- env:<% env.each { v -> %>
  - name : ${v.name}
    value : ${v.value}<% } %>
  project: "${PROJECT}"
  ......
  ......
  ......
  ports:<% ports.each { v -> %>
  - port: ${v.port}
    protocol: ${v.protocol}<% } %>

Then, your main script should look like this:

    def f = new File('output.template')
    def engine = new groovy.text.GStringTemplateEngine()

    def envVar = [
        PROJECT: name,
        env:[
            [name:'param1', value:'value1'],
            [name:'param2', value:'value2'],
            [name:'param3', value:'value3']
        ],
        ports:[
            [protocol:'port1', port:1000],
            [protocol:'port2', port:2000]
        ]
    ]

    def Template = engine.createTemplate(f).make(envVar)

    println "${Template}".trim()

output:

#cat output.template
- env:
  - name : param1
    value : value1
  - name : param2
    value : value2
  - name : param3
    value : value3
  project: "projectName"
  ......
  ......
  ......
  ports:
  - port: 1000
    protocol: port1
  - port: 2000
    protocol: port2

Upvotes: 2

kensou
kensou

Reputation: 11

to navigate through your env variable you can use the following:

envVar.env.each{
    println "name : ${it.name}"
    println "value : ${it.value}"
}

envVar.ports.each{
    println "port : ${it.port}"
    println "protocol : ${it.protocol}"
}

Upvotes: 1

Related Questions