SOLVED

Re: Conditional forEach statement in Velolcity Script

Go to solution
Jon_Wright
Level 4

Re: Conditional forEach statement in Velolcity Script

I owe you so many beers for your help on this! The only thing was I was getting an error if the domain for the lead didn't appear in the JSON which I wasn't getting when just doing a standard foreach, so I referenced another one of you answers​ and wrapped it in an if statement to output some default content if the list is empty, is this the best way to handle this?

#if( $domainProjects.isEmpty() )

Default content

#else

#foreach( $idx in [0..$math.min(7,$math.sub($domainProjects.size(),1))] )

#set( $project = $domainProjects[$foreach.index] )

You're a ${project.category.participantType} for Project ${foreach.count}:

#if( $foreach.index.equals(0) )

#set( $project_category_URL_0 = $domainProjects[0].category.URL.substring(8) )<a href="https://${project_category_URL_0}">##

#elseif( $foreach.index.equals(1) )

#set( $project_category_URL_1 = $domainProjects[1].category.URL.substring(8) )<a href="https://${project_category_URL_1}">##

#elseif( $foreach.index.equals(2) )

#set( $project_category_URL_2 = $domainProjects[2].category.URL.substring(8) )<a href="https://${project_category_URL_2}">##

#elseif( $foreach.index.equals(3) )

#set( $project_category_URL_3 = $domainProjects[3].category.URL.substring(8) )<a href="https://${project_category_URL_3}">##

#elseif( $foreach.index.equals(4) )

#set( $project_category_URL_4 = $domainProjects[4].category.URL.substring(8) )<a href="https://${project_category_URL_4}">##

#elseif( $foreach.index.equals(5) )

#set( $project_category_URL_5 = $domainProjects[5].category.URL.substring(8) )<a href="https://${project_category_URL_5}">##

#elseif( $foreach.index.equals(6) )

#set( $project_category_URL_6 = $domainProjects[6].category.URL.substring(8) )<a href="https://${project_category_URL_6}">##

#elseif( $foreach.index.equals(7) )

#set( $project_category_URL_7 = $domainProjects[7].category.URL.substring(8) )<a href="https://${project_category_URL_7}">##

#end

${project.category.name}##

</a>

<br>

<br>

#end

#end

Thanks again for all of your help.

Jon_Wright
Level 4

Re: Conditional forEach statement in Velolcity Script

Thank you, Sanford! What threw me for a bit in your script was the $project variable in the forEach statement:

#foreach( $project in $allProjects )

As I don't have any elements called $project in the JSON. So I can try and understand VTL a little bit more $project is declaring a variable for each record in the array?!

SanfordWhiteman
Level 10 - Community Moderator

Re: Conditional forEach statement in Velolcity Script

Right.