loop /loop

The second of the two "helper" constructs allows for code repetition, by use of a conditional loop. New in Fusebox 4.1 is the ability to additionally perform a query loop or an index loop.

To perform a conditional loop, the loop /loop construct takes an attribute of condition, much like the if construct. The loop element iterates over all the commands inside the loop until the condition is logically false:

<set name="gunCount" value="1" />
<loop condition="gunCount LTE 21">
    <do action="artillery.salute" />
    <set name="gunCount" value="#(gunCount+1)#" />
</loop>

The developer is responsible for ensuring that the loop's condition becomes false at some point lest an infinite loop occur. To perform a query loop, simply provide the attribute query and pass it the name of a valid query result set. Here's an example of a query loop:

<loop query="getStates">
    <do action="v.showStateInfo" />
</loop>

And finally, to perform an index loop, pass attributes from, to, step, and index. This works the same way that ColdFusion's cfloop does. Here's an example:

<loop from="1" to="5" index="i">
    <do action="v.countToFive"/>
</loop>

New in Fusebox 5.1 (at least when using ColdFusion as your scripting language) is the ability to additionally perform a collection loop or a list loop.

To perform a collection loop, the loop /loop construct requires 2 attributes: collection and item. Example:

<loop collection="#departments#" item="person">
	<include template="dspShowPerson" />
</loop>

To perform a list loop, the loop /loop construct requires 2 attributes: list and index. While the delimiter attribute is not handled in the FB 5.1 core files, it should be a fairly simple process for the developer to add this capability. Example:

<loop list="#alphabet#" index="character">
	<include template="dspShowCharacter" />
</loop>

Note that each loop block can contain only Fusebox grammar verbs; it cannot contain any nested if or loop elements. This was done intentionally since the place for complex logic is in a fuse, not in a circuit.