Array indexing in Confluence / Velocity templates
Asked Answered
N

2

3

Our company has begun using Confluence wiki as a documentation site for our teams, and we are implementing a macro called PocketQuery.

This macro pulls SQL data into the site dynamically (we are pulling information like site contacts).

It uses Velocity Templates to display it's data, however I am having issues when doing simple array indexing.

The code:

#set ( $page = $additionalParams.get('Page') )
#set ( $pages = "" )
#if ( $page != $null && $page != "" )
    #set ( $pages = $page.split(";") )
#else
    #set ( $pages = [] )
#end

The $additionalParams is a list that has been initialised outside of the template, and contains the parameters being passed into macro, in this case:

Page=Site Name;Server

The code I am trying to setup is pulling the Name;Server value from the $additionalParams list, split the value if it is not empty, and then obtain the first value.

I have tried:

$pages.get(1)
$pages[1]

However no value gets pulled (I have also tried zero as an index - same result).

Foreach-ing through this array and printing each entry does work - meaning there are values in there.

All I want to be able to do is index into the array - Can't seem to dig up anyway of doing this.

Could this be converted to a list so it can use the $pages.get method to index into it?

Extending on that would this allow me to use $pages.contains method?

Confluence is using Velocity 1.6.

EDIT:

The solutions on this page and on this page do not work - I am guessing (Wildly) maybe the correct objects are not in the context for the template to use them?

(Would PocketQuery or Confluence be doing this?)

How would I go about using the 'ListTool' ?

Naarah answered 11/6, 2013 at 1:17 Comment(6)
$page != $null || $page != "" seems wrong - I think you want to use && there.Lopes
possible duplicate of What is the best way to access an array inside Velocity?Lopes
You are correct @PaulBellora , Will fix this up, however your second link I have visited before and none of the solutions workNaarah
This might not be the issue, but you would use $pages.get(0) to get the first value, since arrays have zero-based indexes.Lopes
I have tried both already - neither work.Naarah
You might consider asking these kind of question on answers.atlassian.com using the label "addon-de.scandio.confluence.plugins.pocketquer". We're answering questions regarding PocketQuery very regularly there.Transcalent
N
2

The only way I could get this to work with any kind of elegance is as follows:

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

This initialises a seperate array, loops through the split values and adds them to the seperate array, which seems to be able to use the methods provided in this question

I could then proceed to use it thus:

$Pages.get(0)//Would return "Site Name"

And also

$Pages.contains("Site Name")//Would return true
Naarah answered 14/6, 2013 at 4:17 Comment(0)
I
-2

m.t.bennet is actually right - I tested and it works!!! I took about a month to find the asnwer and his code sniplet works

#set ( $Page = $additionalParams.get('Page') )
#set ( $Pages = [] )
#if ( $Page != $null && $Page != "" )
    #foreach($i in $Page.split(";"))
        $Pages.add($i)
    #end
#end
Intertype answered 10/12, 2014 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.