1

I tried to append two hash tables in velocity.

#foreach($dun1 in $dotcontent.pull("+structureName:Checnas +(conhost:fe1d98e8-9699-4f3f-abf5-a6c0afc8ab47 conhost:SYSTEM_HOST)",10,"modDate desc"))
#set($foo={
    $!{dun1.mname}:$!{dun1.subname} 
})
#end

In the above for each loop I am pulling content from structure "Checnas". But at the end we can get only the last value in the content.To solve that we need to append for every iteration.I need syntax for appending hash tables. Please help me to solve this.

Manoj Kumar
  • 745
  • 2
  • 8
  • 29

1 Answers1

2

Your code currently is over writing $foo each time and hence you are just getting the last value. You can use lists in velocity to achieve this. This might work:

#set($listOfMnames=[])
#set($listOfSubNames=[])

#foreach($dun1 in $dotcontent.pull("+structureName:Checnas +(conhost:fe1d98e8-9699-4f3f-abf5-a6c0afc8ab47 conhost:SYSTEM_HOST)",10,"modDate desc"))
#set($foo=$listOfMnames.add($!{dun1.mname}))
#set($foo=$listOfSubNames.add($!{dun1.subname}))
#end

This way, you will end up with two lists 'listOfMnames' and 'listOfSubNames', both fully populated. You can later iterate through them to print/utilise their values.

This link will be helpful and tell you the purpose of using $foo which is not being used and just being assigned. Alternatively, you can also use velocity maps with proper key/val pairs but be sure to declare it before the loop begins.

Community
  • 1
  • 1
Karma-yogi
  • 138
  • 11
  • Yeah thanks for the above answer,it is useful for me.But could you please explain how to declare,initialize and add maps in velocity. – Manoj Kumar Jul 06 '15 at 09:38
  • You can find your answer [here](http://stackoverflow.com/questions/16398116/velocity-template-engine-key-value-map) and further information in this [question](http://stackoverflow.com/questions/9964075/how-to-access-map-in-velocity-template-file) – Karma-yogi Jul 06 '15 at 18:46