Tip: Put separators only "in-between" slot items

Motivation: Out-of-box #slot macro allows header, footer, snippet header, and snippet footer to be rendered around slot items. This is very useful, e.g. when slot items are placed in table cells or bullet lists. However, in page header or footer, it is common to put separators only in-between links, e.g.

link1 | link2 | link3 | link4 | link5

This posting suggests a convenient way to do this.

Approach: Continue to use the out-of-box #slot macro, but use $velocityCount loop counter and string interpolation to output the separators only in-between items.

Solution: At first thought, one may consider writing a custom slot macro to archive the result. However, there is a simpler way to do this:

#slot(‘slotname’ ‘<div>’ “#if ($velocityCount > 1)  |;  #end” ‘’ ‘</div>’ ‘’)

$velocityCount is a built-in loop counter for #foreach. As expected, we print the separator only when it is not in the first iteration. The important part is the line “#if ($velocityCount > 1)  |;  #end” is surrounded by double-quotes to trigger the velocity’s string interpolation.