Adding Velocity Foreach Conditionals

Est. Reading Time: 1 minute

There are different approaches to adding conditionals in a foreach loop. These are just a few code examples that are simple and easy to integrate in Velocity.

Note the examples would are what go in the foreach loop. Your set would be applied above with your specific variables.

Example 1: Gets the last value in the foreach loop and applying HTML or code element.

#if ( $foreach.count == $group.size() )

HTML can be placed here.

#end

Example 2: Gets any value except that last one in the foreach loop and applying HTML or code element.

#if ( $foreach.count != $foreachGroup.size() )

HTML can be placed here.

#end

Example 3: Gets any value with a specific count and then uses #break. (Note: #break in a foreach loop is available in version 1.6 or higher)

#if ( $foreach.count > 3 )

#break

#end

Zedric Myers
Zedric Myers is a Web Designer for Beacon Technologies. After earning his degree in Advertising and Graphic Design, he spent 11 years with an advertising agency in Greensboro, NC where he was initially hired as a designer and transitioned to being a Web Developer.
Load More Posts