Take the 2-minute tour ×

I would like to know if it is possible to construct name of variable into velocity dynamically.

i.e. lets say I've 6 variables into velocity template [name1, name2, name3 .. name6] I would like to output them. So I'm looking in something like:

#foreach ( $counter in [1..6] )

is it possible somehow?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You could construct a map and build the names of the keys to retrieve the values you want:

#set( $map = {"${name}1":'value1', "${name}2":'value2'} )

    #set( $key = "${name}$counter" )
share|improve this answer
map is fine, thanks. –  Dmytro Jun 13 '13 at 12:06

It is possible using the #evaluate directive:

#evaluate ('$name1')

#set ($d = '$')
#foreach ($i in [1..6])
  #set ($varName = "${d}name${i}")
share|improve this answer
thanks. nice. did not know that. –  Dmytro Jun 14 '13 at 14:21

Your Answer


