Conditional Evaluation

From Documentation


Conditional Evaluation


If and Unless

The evaluation of an element could be conditional. By specifying the if, unless attribute or both, developers could control whether to evaluate the associated element. It is also the most straightforward way.

For example, suppose that we want to use label, if readonly, and textbox, otherwise:

<label value="${customer.label}" if="${param.readonly == 'true'}"/>
<textbox value="${customer.value}" unless="${param.readonly == 'true'}"/>

Here is another example: window is created only if a is 1 and b is not 2. If an element is ignored, all of its child elements are ignored too.

<window if="${a==1}" unless="${b==2}">
    ...    
</window>

Switch and Case

With the switch and case attributes of the zk element, you can evaluate a section of a ZUML document only if a variable matches a certain value. It is similar to Java's switch statement.

For example,

<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 section of a ZUML document 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 Expressions

Regular expressions are allowed in the case attribute too, as shown below.

<zk switch="${fruit}">
    <zk case="/ap*.e/">
    Evaluate if the regular expression, ap*.e"., matches the switch condition.
    </zk>
</zk>

Used with forEach

Like any other elements, you can use with the forEach attribute (so are if and unless). The forEach attribute 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">

Choose and When

The choose and when attributes of the zk element is the third approach of conditional evaluation.

As shown below, it is enclosed with a zk element with the choose attribute, and ZK Loader will evaluate its child elements (the zk elements with the when attribute) one-by-one until the first one matches:

<zk choose="">
    <zk when="${fruit == 'apple'}">    
    Evaluated if the when condition is true.
    </zk>
    <zk><!-- default -->
    Evaluated if none of above cases matches.
    </zk>
</zk>

You don't have to assign any value to the choose attribute, which is used only to identify the range of the mutually exclusive conditional evaluation.

Version History

Last Update : 2011/09/16


Version Date Content
     



Last Update : 2011/09/16

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.