Adding Velocity Foreach Conditionals

Beacon Blog Article

By Zedric Myers | Published January 9, 2020 | Categories: Cascade CMS

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

Let's get to work!

Contact Us