Table of Contents
if /if
The first of two "helper" constructs for the Fusebox grammar is the conditional if /if. This is a straight forward way to provide branch logic within your application. Keep in mind that, all else being equal, you should strive to keep program logic in your fuses rather than your fuseactions. There are occasions, however, when it's clearer to do this in a fuseaction; hence the existence of this construct.
The if /if element takes one attribute, condition, that is required. It contains a statement that can be evaluated as stated by the script language being used for the core files to either true or false. The if /if construct also offers the sub-element tag pairs true /true and false /false. Both are optional and can contain Fusebox grammar instructions for what to do when the condition specified evaluates to true or false.
Here's an example of an if /if:
<if condition="listUsers.recordCount GT 0">
<true>
<xfa name="editUser" value="users.addEditUser" />
<include template="dspListUsers" />
</true>
<false>
<include template="dspNoRecordsFound.cfm" />
</false>
</if>
Notice that we can put any of the actual Fusebox grammar verbs inside the true and false tagset blocks. If you only wish to run code only when the condition is in a desired state (true or false), you can omit the unnecessary conditional. Here is code that is intended to run only when the stated condition is true.
<if condition="listUsers.recordCount GT 0">
<true>
<xfa name="editUser" value="users.addEditUser" />
<include template="dspListUsers" />
</true>
</if>
No false /false block is required. Similarly the true /true block can be omitted and it is legal to write:
<if condition="listUsers.recordCount GT 0">
<false>
<include template="dspNoRecordsFound.cfm" />
</false>
</if>
In PHP Fusebox your condition text is used as-is in the parsed if ( ){ } statement, so you can use regular PHP code (remember to escape any xml entities!)
<if condition="mysql_num_rows($listUsers) > 0">
<false>
<include template="dspNoRecordsFound.php" />
</false>
</if>
Note that in Fusebox 4 or 4.1 each true and false block cannot contain any additional nested ifs or loops (discussed in the next section). This is limitation is removed in Fusebox 5.
As we've reiterated several times you should however strive to put program logic in your fuses rather than your fuseactions whenever possible.
