Conditional Evaluation"

From Documentation
Line 20: Line 20:
 
=Switch and Case=
 
=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.
+
With the <tt>switch</tt> and <tt>case</tt> attributes of [[ZUML Reference/ZUML/Elements/zk|the <tt>zk</tt> 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,
 
For example,

Revision as of 08:59, 7 December 2010


Conditional Evaluation


If and Unless

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

For example, suppose 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 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 provide an alternative way for mutually exclusive conditional evaluation.

<zk choose="">
    <zk when="${fruit == 'apple'}">    
    Evaluated if the when condition is true.
    </zk>
    <zk>
    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 : 2010/12/07


Version Date Content
     



Last Update : 2010/12/07

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