ATG ForEach Output with Separators Between Items

Pez Collection

Sometimes you want to list each item in the collection with a separator in between like a comma or slash.  However the trick is to have the separator only in between items.

In ATG DSP you can use the ForEach droplet to iterate through each item in a collection.  However to put a separator between each item is not straight-forward with this droplet.  To do this I used JSTL to test if we are at the end of the collection.  If we are then I don’t add the separator, otherwise I do.

Here is an example.


And here is the output for this example.

The Incredibles, Ratatouille, Cars

Do you have a better or more elegant solution?  Please feel free to let me know in the comments?

2 thoughts on “ATG ForEach Output with Separators Between Items

  1. I can remove 3 lines:

    <dspel:droplet name="/atg/dynamo/droplet/ForEach">
    <dspel:param name="array" param="dvds"/>
    <dspel:param name="elementName" value="dvd" />
    <dspel:oparam name="output">
    <dspel:getvalueof var="count" param="count" />
    <c:if test="${1 != count}"> , </c:if>
    <dspel:valueof param="dvd.title" valueishtml="true"/>

    I just do the test before the text as opposed to after, so you don’t need to know how long the list is.

    I also use the elementName param. I’ve not see then setvalue used in the way you’ve done before.

    And finally, I’ve often thought of subclassing ForEach to allow an inbetween oparam. That’s left as an exercise for the reader.

  2. Piran, that’s a great solution! I especially like the idea of subclassing ForEach to allow an inbetween oparam.


Leave a Reply

Your email address will not be published. Required fields are marked *