How do I create a list?

I am working on a template and need to be able to keep a list of content IDs that have already been added to the page. This is so that in another area of the page where we add a different variant of the same content type we prevent the same content ID from being added twice.

Thanks for any advice and help you can give.

one way to create a list is use $user.PSOListTool.asList().

thank you that helps.

With that, what would you do to add items to that list once a variable holding that list was created?

You can use the add() method. See the Java Doc for java.util.list:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html#add(E)

There are some other methods, including addAll(). They are described in the JavaDoc page for List.


$foo = $user.PSOListTool.asList("abc")
       $foo.add("def")

$foo is now a list with 2 elements: “abc”, “def”

Dave

I thought I tried that but maybe I did something wrong. I’ll try again, thank you.

Hi there I have tried following this process to create a list but it doesn’t work?
I have the PSO toll installed but can’t seem to create a list.

I usually use something like…

#set($myarray = [0])##
$myarray.clear()##

…to create an empty array, then call the add() method on it to add elements to it.

Hey presto

That appears to work fine!

Thank you very much that has had me stumped so far!

Dave:

Just trying this out. If you use this is a Velocity template how do you get it to not render the ‘true’ return value of the .add
method? It looks like there is version of add that won’t do this if you specify the index prior to the element like so:

#set($foo = $user.psoListTools.asList(“abc”))
$foo.add(1, “def”)

But I can’t seem to get this to work in a for loop with index provided by $velocityCount - 1 or something along those lines. Any suggestions?

[ul]
[li]What version of Percussion CM are you using?[/li][li]Do you encounter any error messages?[/li][li]Can you provide a snippet of the code you’re attempting to use?[/li][/ul]