Looping through child table entries in template velocity code

Hi,
What is the code I should use to loop through the child table entries in a content type ?

For example, assume I have a content type ct1 with a child table field (sys_Table) called myChild. The myChild has two fields ‘pname’ and ‘pvalue’ (both sys_EditBox).
In my template velocity code I want to say something like (pseudocode):
<code>
foreach(childRec in myChild)
{
$pname + “=” + $pvalue + “,”
}
</code>

so that i end up with:
“city=seattle,state=WA,country=USA,”

The problem with this macro:

<code>#children(childname template header beforetext aftertext $footer)</code>

is that it requires a template and if i have 10 different child tables requiring different formatting, I need to have 10 different templates. Seems like too much clutter to me and prefer doing it in code.

If you don’t already have a solution, give this a try:


#set($myChild_pname_array = $rx.asmhelper.childValues($sys.item,"myChild","pname"))
#set($myChild_pvalue_array = $rx.asmhelper.childValues($sys.item,"myChild","pvalue"))
#set($myString = "")
#foreach ($i in [1..$myChild_pname_array.size()])
   #set($index = $i - 1)
   #set ($myString = $myString + $myChild_pname_array.get($index).string + "=" +  $myChild_pvalue_array.get($index).string)
   #if ($i != $myChild_pname_array.size())
      #set ($myString = $myString + ", ")
   #end
#end
$myString

This creates two arrays for the pname and pvalue fields which you loop through, format, and print. This of course assumes that the 2 fields are required and always have a value. You may want/need to add string length tests to make sure there is content in the fields. I added the if statement so that a comma is only added between two items and not after the last item.

Regards,
Endora

See this: Stepping through Child Table Rows

Thanks. The solution works and is helpful since it does not require usage of a template.