The <else>, <elseif>, and
<elseifnot> tags aid in the creation of more involved conditional logic than a
single <if> or <ifnot> statement.
The <else> tag marks a region to be displayed only if the last logical
evaluation (at the same logical
depth
, if working with nested logic tags) was
false. If the last logical evaluation was true, the
<else> region will be muted.
Example 13-17 creates a simple <if> condition to check for
the existence of a variable called answer. If it is not found, the region marked up by
the <else> and </else> tags will be
displayed; otherwise, that region will be muted.
Example 13-17. Using the <else> tag
<lxp>
<if answer>
Thank you for supplying an answer.
</if>
<else>
You have not yet supplied an answer.<br />
<include src="forms/question.lxp" />
</else>
</lxp>
As mentioned earlier in this section, the <elseif> and
<elseifnot> tags are just shortcuts. They behave exactly as the
<if> and <ifnot> tags do, respectively, if
they were nested within an <else> region. For example, the following two blocks of
markup are functionally identical:
<if condition1="true">
Condition 1 is True.
</if>
<else>
<if condition2="true">
Condition 2 is true.
</if>
</else>
...
<if condition1="true">
Condition 1 is True.
</if>
<elseif condition2="true">
Condition 2 is true.
</elseif>
Using <else> tags streamlines both the maintainability and efficiency of the
conditional logic. By using <else>, you can rely on LXP to keep track of whether or
not the last condition was or was not met, and not have to re-evaluate the same conditions with the opposite logic
tag.
Example 13-18 re-implements the same logic that was used in Example 13-15 earlier in this section, but improves it with the use of the
<else> tag.
Example 13-18. Using nested logic with <else> tags
<lxp>
<if answer>
<strong>You have supplied an answer!</strong><br />
<if answer="12">
Your answer is correct!<br />
<if cheatcode>
You appear to be cheating, however.
</if>
<else>
Congratulations for not cheating!
</else>
</if>
<else>
Your answer of <putvar name="answer">, though, is incorrect.<br />
</else>
</if>
<else>
You have not yet supplied an answer.<br />
<include src="forms/question.lxp" />
</else>
</lxp>