Jump to a location with in a code block, based in an input value.
This tag takes a value (on
) and jumps to a <case> in the enclosed block that has a matching value.
If there is no matching <case>, then execution will move to the enclosed <default-case>. If no default case exists, the entire block is skipped.
This tag is useful in situations where a chain of <if> and <elif> tags would otherwise be used. Here's an example:
<switch on="species"><case>human</case><echo>Humans come from Earth</echo><case>hynerian</case><echo>Hynerians come from Hyneria</echo><default-case/><echo>I have no idea where ${species}s come from</echo></switch>
The above code is equivalent to the following:
<if test="species=='human'"><echo>Humans come from Earth</echo></if><elif test="species=='hynerian'"><echo>Hynerians come from Hyneria</echo></elif><else><echo>I have no idea where ${species}s come from</echo></else>
The <switch> version is generally clearer, especially with a large number of conditions.
If the on
attribute is not given, then the case's if
attribute is checked. Here's the previous example, implemented without the on
attribute:
<switch><case if="species=='human'"/><echo>Humans come from Earth</echo><case if="species=='hynerian'"/><echo>Hynerians come from Hyneria</echo><default-case/><echo>I have no idea where ${species}s come from</echo></switch>
name | purpose | type | required? | default | choices |
---|---|---|---|---|---|
on |
Value to test | expression | No | None |
name | purpose | type | required? | default | choices |
---|---|---|---|---|---|
if |
Conditional expression | expression | No | yes |