Home   Single Page

Switch and Case

With the switch and case attributes of the zk element, you can evaluate a portion of a ZK page only if a variable matches a certain value.

<zk switch="${fruit}">
    <zk case="apple">    
    Evaluated only if ${fruit} is apple    

</zk>

<zk case="${special}">

Evaluated only if ${fruit} equals ${special}

</zk>

<zk>

Evaluated only if none of the above cases matches.

</zk>

</zk>

ZK Loader will evaluate from the first case to the last case, until it matches the switch condition, which is the value specified in the switch attribute. The evaluation is mutually exclusive conditional. Only the first matched case is evaluated.

The zk element without any case, is the default – i.e., it always matches and is evaluated if all cases above it failed to match.

Multiple Cases

You can specify a list of cases in one case attribute, such that a portion of a ZK page has to be evaluated if one of them matches.

<zk switch="${fruit}">
    <zk case="apple, ${special}">    
    Evaluated if ${fruit} is either apple or ${special}    

</zk>

</zk>

Regular Expression

<zk switch="${fruit}">

<zk case="/ap*.e/">

Evaluate if the regular expression, ap*.e"., matches the switch condition.

</zk>

</zk>

Used with forEach

Like other elements, you can use with the forEach attribute (so are if and unless). The forEach condition is evaluated first, so the following is the same as multiple cases.

<zk case="${each}" forEach="apple, orange">

is equivalent to

<zk case="apple, orange">