Tag: foreach
How to Debug No Output for ATG ForEach
by Frank Kim on Jul.28, 2009, under Page Development
Today I added a feature in my shopping cart JSP page but nothing was showing up. This was because the output of ATG ForEach droplet was blanks.
To debug this I did the following steps.
- I added an empty oparam to see if the ForEach droplet thought the collection was empty.
<dsp:oparam="empty">Empty?</dsp:oparam>
It did not.
- I outputted the count in the output oparam to see if it was looping through the collection.
<dsp:oparam="output"><dsp:valueof param="count"/> <dsp:valueof param="element"/></dsp:oparam>
It was looping through the collection.
- Finally I debugged using Eclipse the ForEach code. The ForEach code can be found in <ATG>/DAS/src/Java/atg/droplet/ForEach.java.
Using the debugger I realized that the element name of the loop was being set to something else which is why <dsp:valueof param=”element”/> was blank.
It turned out that the ForEach droplet was inside another ForEach droplet and in that droplet they set the element name like this.
<dsp:param name="elementName" value="CommerceItem" />
By setting the elementName parameter to something else in my ForEach droplet and then referencing it properly I was finally able to get the expected output.
<dsp:droplet name="/atg/dynamo/droplet/ForEach"> <dsp:param name="array" param="CommerceItem.auxiliaryData.productRef.dvds"/> <dsp:param name="elementName" value="dvd" /> <dsp:oparam name="output"> <dsp:valueof param="dvd.name" /> </dsp:oparam> </dsp:droplet>
By the way ATG documentation suggests not using elementName.
elementNameandindexNameallow you to provide a parameter name other thanelementandindex, respectively. A better way to provide a different parameter name is to use thedsp:setvaluetag.
In our example we would do this instead of setting the elementName param.
<dsp:setvalue param="dvd" paramvalue="element" />
ATG ForEach Output with Separators Between Items
by Frank Kim on Jul.24, 2009, under Page Development
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.
<dspel:droplet name="/atg/dynamo/droplet/ForEach">
<dspel:param name="array" param="dvds"/>
<dspel:setvalue param="dvd" paramvalue="element" />
<dspel:oparam name="outputStart">
<dspel:getvalueof var="size" param="size" />
</dspel:oparam>
<dspel:oparam name="output">
<dspel:valueof param="dvd.title" valueishtml="true"/>
<dspel:getvalueof var="count" param="count" />
<c:if test="${size != count}"> , </c:if>
</dspel:oparam>
</dspel:droplet>
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?

