25

I have a Java array such as:

String[] arr = new String[] {"123","doc","projectReport.doc"};

In my opinion the natural way to access would be:

 #set($att_id = $arr[0])
 #set($att_type = $arr[1])
 #set($att_name = $arr[2])

But that it is not working. I have come with this workaround. But it a bit too much code for such an easy task.

#set($counter = 0)
#foreach($el in $arr)
    #if($counter==0)
        #set($att_id = $el)
    #elseif($counter==1)
        #set($att_type = $el)
    #elseif($counter==2)
         #set($att_name = $el)
    #end
    #set($counter = $counter + 1)
#end

Is there any other way?

Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179

8 Answers8

27

You can use use Velocity 1.6: for an array named $array one can simply do $array.get($index).

In the upcoming Velocity 1.7, one will be able to do $array[$index] (as well as $list[$index] and $map[$key]).

Chris
  • 26,544
  • 5
  • 58
  • 71
Nathan Bubna
  • 6,823
  • 2
  • 35
  • 36
10

You could wrap the array in a List using Arrays.asList(T... a). The new List object is backed by the original array so it doesn't wastefully allocate a copy. Even changes made to the new List will propagate back to the array.

Then you can use $list.get(int index) to get your objects out in Velocity.

If you need to get just one or two objects from an array, you can also use Array.get(Object array, int index) to get an item from an array.

starwarswii
  • 2,187
  • 1
  • 16
  • 19
Brian
  • 8,454
  • 5
  • 27
  • 30
2
String[] arr = new String[] {"123", "doc", "projectReport.doc"}; 

In my opinion the natural way to access would be:

 #set($att_id = $arr[0]) 
 #set($att_type = $arr[1]) 
 #set($att_name = $arr[2]) 

The value for this can be get by using $array.get("arr", 1) because there is no direct way to get the value from array like $att_id = $arr[0] in velocity.
Hope it works :)

LPL
  • 16,827
  • 6
  • 51
  • 95
2

Velocity 1.6

$myarray.isEmpty()
$myarray.size()
$myarray.get(2)
$myarray.set(1, 'test')

http://velocity.apache.org/engine/1.7/user-guide.html

trndjc
  • 11,654
  • 3
  • 38
  • 51
1

there is an implicit counter $velocityCount which starts with value 1 so you do not have to create your own counter.

1

Brian's answer is indeed correct, although you might like to know that upcoming Velocity 1.6 has direct support for arrays; see the Velocity documentation for more information.

starwarswii
  • 2,187
  • 1
  • 16
  • 19
Angelo van der Sijpt
  • 3,611
  • 18
  • 24
  • 1
    It's true. In 1.6-beta1 and later, you can just call most ArrayList methods on your array objects. e.g. $array.get(0) $array.set(0, 'foo') – Nathan Bubna Oct 17 '08 at 22:58
0

I has the same question and it got answered on another thread

#set ( $Page = $additionalParams.get('Page') )
#set ( $Pages = [] )
#if ( $Page != $null && $Page != "" )
    #foreach($i in $Page.split(";"))
        $Pages.add($i)
    #end
#end

Array indexing in Confluence / Velocity templates

  • Can anyone answer this http://stackoverflow.com/questions/31116553/how-to-append-hash-tables-in-velocity-template – Manoj Kumar Jun 30 '15 at 06:37
0

I ended up using the ListTool from the velocity-tools.jar. It has methods to access an array's elements and also get its size.

Luke Quinane
  • 16,447
  • 13
  • 69
  • 88