Display a list of directory items separated by commas using conditionals

Jun 22, 2011 12:12 pm

When you display lists of directory items on a site, you often want some kind of separator between the items, like commas or separator lines. However, the first or last item is often different; for example, you wouldn't want a comma after the last item.

1. This little trick will let you list directory items with a comma between each item in a lister:

Place a comma before each item, and use a conditional statement to remove it from the first item in the list.

For example:

<if ([[#]] != 1)>,&nbsp;</if>[[item_name]]

which, in the example of a directory of tools, would display something like:


2. A similar example (that doesn't use conditionals) puts a separating line between each item in a vertical list. You'd do something like:

<div class="item item[[#]]">[[item_name]]</div>

and your CSS would be:

.item { border-top: 1px solid gray; }
.item1 { border: 0; }

This would display something like:

For a complete list of the conditionals you can use in your module layouts, see the "Using Conditional Statements" page in the Green help documentation.