Adding Velocity Foreach Conditionals

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

Contact Us To Find Out More

Have questions? Need help? Contact us and we'll be in touch soon!

* Required

Beacon
164 Thatcher Road
Greensboro, NC 27409
Map and Directions

Sales & Marketing
336.232.5687