<if>
The <if>
construct is used within block code to control what Macro
Language code is included during expansion given a particular condition.
<if>
block code construct is different from the 1010data
function if(C1;R1;C2;R2;...;D)
that is part of the expression language.
For more information, see the Additional information section below.Syntax
<if test="{[CONDITION]}"> <then> [CONTENTS_1] </then> <else> [CONTENTS_0] </else> </if>
Alternate syntax
<if test="{[CONDITION]}"> [CONTENTS_1] </if>
Description
The form:
<if test="{X}">
<then>
[CONTENTS_1]
</then>
<else>
[CONTENTS_0]
</else>
</if>
expands to:
[CONTENTS_1]
if X
evaluates to 1 and:
[CONTENTS_0]
if X
evaluates to 0.
The <else>
clause may be omitted, in which case nothing is inserted if
X
evaluates to 0. For example:
<if test="{X}">
[CONTENTS_1]
</if>
<then>
tags are not necessary. Attributes
test
- For
test="[CONDITION]"
, the [CONDITION] must resolve to a 1 or 0.
Example
In this example, a block is created to parameterize the store selection variable. The
test
attribute checks to make sure the value provided is within
acceptable limits, and then a selection statement is carried out, assuming the test resolves
to 1
.
<base table="pub.demo.retail.store"/> <block name="store_select" store_id="2"> <if test="{@store_id<1 | @store_id>3}"> <then> <signal msg="That is not a valid store selection for this dataset"/> </then> <else> <sel value="store_id={@store_id}"/> </else> </if> </block>
Additional information
The <if>
construct is different from the 1010data function
if(C1;R1;C2;R2;...;D)
that can be used in 1010data expressions.
The <if>
construct in block code takes a single condition and expands
its contents only if the condition is true. If it contains <then>
and
<else>
clauses, it expands the contents of the
<then>
clause if the test condition is true, otherwise it expands the
contents of the <else>
clause.
The if(C1;R1;C2;R2;...;D)
function in the expression language, on the
other hand, takes a series of conditions and returns the value corresponding to the first
condition that evaluates to true (or the final value if none of the conditions are true).
For more information, see if(C1;R1;C2;R2;...;D)
.
Note that the block code <switch>
statement implements behavior
analogous to that of the if(C1;R1;C2;R2;...;D)
function. For more
information, see <switch>
.