Conditional Evaluation"

From Documentation
m (remove empty version history (via JWB))
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
 
=If and Unless=
 
=If and Unless=
The component creation can be conditional. By specifying the <tt>if</tt>, <tt>unless</tt> attribute or both, developers can control whether to create the associated component. It is also the most straightforward way.
+
The component creation can be conditional. By specifying the <code>if</code>, <code>unless</code> attribute or both, developers can control whether to create the associated component. It is also the most straightforward way.
  
 
For example, suppose that we want to use [[ZK Component Reference/Essential Components/Label|label]], if readonly, and [[ZK Component Reference/Input/Textbox|textbox]], otherwise:
 
For example, suppose that we want to use [[ZK Component Reference/Essential Components/Label|label]], if readonly, and [[ZK Component Reference/Input/Textbox|textbox]], otherwise:
Line 19: Line 19:
 
</window>
 
</window>
 
</source>
 
</source>
* [[ZK Component Reference/Containers/Window|window]] is created only if <tt>a</tt> is 1 and <tt>b</tt> is not 2.
+
* [[ZK Component Reference/Containers/Window|window]] is created only if <code>a</code> is 1 and <code>b</code> is not 2.
  
 
=Switch and Case=
 
=Switch and Case=
  
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.
+
With the <code>switch</code> and <code>case</code> attributes of [[ZUML Reference/ZUML/Elements/zk|the <code>zk</code> 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,
Line 58: Line 58:
 
== Regular Expressions ==
 
== Regular Expressions ==
  
Regular expressions are allowed in the <tt>case</tt> attribute too, as shown below.
+
Regular expressions are allowed in the <code>case</code> attribute too, as shown below.
  
 
<source lang="xml">
 
<source lang="xml">
Line 70: Line 70:
 
== Used with forEach ==
 
== Used with forEach ==
  
Like any other elements, you can use the [[ZK Developer's Reference/UI Composing/ZUML/Iterative Evaluation|the forEach attribute]] (so are if and unless). The <tt>forEach</tt> attribute is evaluated first, so the following is the same as multiple cases.
+
Like any other elements, you can use the [[ZK Developer's Reference/UI Composing/ZUML/Iterative Evaluation|forEach attribute]] (so are if and unless). The <code>forEach</code> attribute is evaluated first, so the following is the same as multiple cases.
  
 
<source lang="xml">
 
<source lang="xml">
Line 84: Line 84:
 
=Choose and When=
 
=Choose and When=
  
The <tt>choose</tt> and <tt>when</tt> attributes of [[ZUML Reference/ZUML/Elements/zk|the <tt>zk</tt> element]] is the third approach of conditional evaluation.
+
The <code>choose</code> and <code>when</code> attributes of [[ZUML Reference/ZUML/Elements/zk|the <code>zk</code> element]] are the third approach of conditional evaluation.
  
As shown below, it is enclosed with a <tt>zk</tt> element with the <tt>choose</tt> attribute, and the ZK Loader will evaluate its child elements (the <tt>zk</tt> elements with the <tt>when</tt> attribute) one-by-one until the first one matches:
+
As shown below, it is enclosed with a <code>zk</code> element with the <code>choose</code> attribute, and the ZK Loader will evaluate its child elements (the <code>zk</code> elements with the <code>when</code> attribute) one-by-one until the first one matches:
  
 
<source lang="xml">
 
<source lang="xml">
Line 99: Line 99:
 
</source>
 
</source>
  
You don't have to assign any value to the <tt>choose</tt> attribute, which is used only to identify the range of the mutually exclusive conditional evaluation.
+
You don't have to assign any value to the <code>choose</code> attribute, which is used only to identify the range of the mutually exclusive conditional evaluation.
 +
 
  
=Version History=
 
{{LastUpdated}}
 
{| border='1px' | width="100%"
 
! Version !! Date !! Content
 
|-
 
| &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|}
 
  
 
{{ZKDevelopersReferencePageFooter}}
 
{{ZKDevelopersReferencePageFooter}}

Latest revision as of 05:54, 6 February 2024


Conditional Evaluation


If and Unless

The component creation can be conditional. By specifying the if, unless attribute or both, developers can control whether to create the associated component. 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'}"/>

Besides, if a parent component is ignored (not created), all of its child components are ignored too.

Here is another example:

<window if="${a==1}" unless="${b==2}">
    ...    
</window>
  • window is created only if a is 1 and b is not 2.

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 the 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 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 are the third approach of conditional evaluation.

As shown below, it is enclosed with a zk element with the choose attribute, and the 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.




Last Update : 2024/02/06

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