Difference between revisions of "ZUML ZK Elements"

From Documentation
(Created page with '{{ZKDevelopersGuidePageHeader}} == Overview == ZK elements are used to control ZUML pages other than creating components. == The <tt>zk</tt> Element == <source lang="xml" > …')
 
m (correct highlight (via JWB))
 
Line 4: Line 4:
 
ZK elements are used to control ZUML pages other than creating components.
 
ZK elements are used to control ZUML pages other than creating components.
  
== The <tt>zk</tt> Element ==
+
== The <code>zk</code> Element ==
 
   
 
   
 
<source lang="xml" >
 
<source lang="xml" >
Line 10: Line 10:
 
</source>
 
</source>
 
   
 
   
It is a special element used to aggregate other components. Unlike a real component (say, <tt>hbox</tt> or <tt>div</tt>), it is not part of the component tree being created. In other words, it doesn't represent any component. For example,
+
It is a special element used to aggregate other components. Unlike a real component (say, <code>hbox</code> or <code>div</code>), it is not part of the component tree being created. In other words, it doesn't represent any component. For example,
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 33: Line 33:
  
 
=== Multiple Root Elements in a Page ===
 
=== Multiple Root Elements in a Page ===
Due to XML's syntax limitation, we can only specify one document root. Thus, if you have multiple root components, you must use <tt>zk</tt> as the document root to group these root components.<ref>A component without any parent is called a root component</ref>
+
Due to XML's syntax limitation, we can only specify one document root. Thus, if you have multiple root components, you must use <code>zk</code> as the document root to group these root components.<ref>A component without any parent is called a root component</ref>
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 48: Line 48:
  
 
=== Iteration Over Versatile Components ===
 
=== Iteration Over Versatile Components ===
The <tt>zk</tt> element, like components, supports the <tt>forEach</tt> attribute. Thus, you could use it to generate different type of components depending on the conditions. In the following example, we assume <tt>mycols</tt> is a collection of objects that have member, <tt>isUseText()</tt>. For each object in <tt>mycols</tt>, a <tt>textbox</tt> will be created if  isUseText() returns true. <tt>useText</tt> is auto transformed to <tt>isUseText()</tt> by EL expression evaluation.
+
The <code>zk</code> element, like components, supports the <code>forEach</code> attribute. Thus, you could use it to generate different type of components depending on the conditions. In the following example, we assume <code>mycols</code> is a collection of objects that have member, <code>isUseText()</code>. For each object in <code>mycols</code>, a <code>textbox</code> will be created if  isUseText() returns true. <code>useText</code> is auto transformed to <code>isUseText()</code> by EL expression evaluation.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 58: Line 58:
 
</source>
 
</source>
  
For more information about <tt>forEach</tt>, <tt>if</tt>, please refer to section The ZK Attributes.
+
For more information about <code>forEach</code>, <code>if</code>, please refer to section The ZK Attributes.
  
 
'''Notes'''
 
'''Notes'''
 
<references/>
 
<references/>
  
== The <tt>zscript</tt> Element ==
+
== The <code>zscript</code> Element ==
 
=== Write java code in ZUML ===
 
=== Write java code in ZUML ===
 
  <zscript>''Scripting codes''</zscript>
 
  <zscript>''Scripting codes''</zscript>
Line 70: Line 70:
 
It defines a piece of the scripting codes, say the Java codes, that will be interpreted when the page is evaluated. The language of the scripting codes is, by default, Java.  
 
It defines a piece of the scripting codes, say the Java codes, that will be interpreted when the page is evaluated. The language of the scripting codes is, by default, Java.  
  
The <tt>zscript</tt> element has two formats as shown above. The first format is used to embed the scripting codes directly in the page. The second format is used to reference an external file that contains the scripting codes.
+
The <code>zscript</code> element has two formats as shown above. The first format is used to embed the scripting codes directly in the page. The second format is used to reference an external file that contains the scripting codes.
  
You can also embed <tt>zscript</tt> in event handler in ZUML. In the following example,  
+
You can also embed <code>zscript</code> in event handler in ZUML. In the following example,  
 
<source lang="xml" >
 
<source lang="xml" >
 
  <button onClick="alert(&quot;Hi&quot;)"/>
 
  <button onClick="alert(&quot;Hi&quot;)"/>
 
</source>
 
</source>
  
<tt><nowiki>alert(&quot;quot;Hi&quot;quot;)</nowiki></tt>is also a piece of <tt>zscript</tt> code.
+
<code><nowiki>alert(&quot;quot;Hi&quot;quot;)</nowiki></code>is also a piece of <code>zscript</code> code.
  
 
{| border="1"
 
{| border="1"
Line 92: Line 92:
 
|-
 
|-
 
|  deferred
 
|  deferred
| [Optional][Default: <tt>false</tt>]
+
| [Optional][Default: <code>false</code>]
  
Whether to defer the evaluation of this element until the first non-deferred <tt>zscript</tt> codes of the same language need to be evaluated. Refer to the '''How to Defer the Evaluation''' section below.
+
Whether to defer the evaluation of this element until the first non-deferred <code>zscript</code> codes of the same language need to be evaluated. Refer to the '''How to Defer the Evaluation''' section below.
  
 
|}
 
|}
  
 
=== How to Defer the Evaluation ===
 
=== How to Defer the Evaluation ===
ZK loads the interpreter before it is going to evaluate the first <tt>zscript</tt> codes. For example, the Java interpreter is loaded when the user clicks the button in the following example.
+
ZK loads the interpreter before it is going to evaluate the first <code>zscript</code> codes. For example, the Java interpreter is loaded when the user clicks the button in the following example.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 105: Line 105:
 
</source>
 
</source>
 
   
 
   
On the other hand, the interpreter is loaded when loading the following ZUML page, since the <tt>zscript</tt> element needs to be evaluated when loading the page.
+
On the other hand, the interpreter is loaded when loading the following ZUML page, since the <code>zscript</code> element needs to be evaluated when loading the page.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 117: Line 117:
 
</source>
 
</source>
 
   
 
   
If you prefer to defer the loading of the interpreter, you can specify the <tt>deferred</tt> option with true. Then, the interpreter won't be loaded, until the user clicks the button.
+
If you prefer to defer the loading of the interpreter, you can specify the <code>deferred</code> option with true. Then, the interpreter won't be loaded, until the user clicks the button.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 129: Line 129:
 
</source>
 
</source>
 
   
 
   
'''Note''': The evaluation of EL expressions specified in the <tt>if</tt>, <tt>unless</tt> and <tt>src</tt> attributes are also deferred.
+
'''Note''': The evaluation of EL expressions specified in the <code>if</code>, <code>unless</code> and <code>src</code> attributes are also deferred.
  
'''Note''': If the component is detached from the page by the time the interpreter is loaded, the <tt>zscript</tt> codes are ignored. For example, if the window in the previous example no longer belongs to the page, the deferred <tt>zscript</tt> won't be interpreted.
+
'''Note''': If the component is detached from the page by the time the interpreter is loaded, the <code>zscript</code> codes are ignored. For example, if the window in the previous example no longer belongs to the page, the deferred <code>zscript</code> won't be interpreted.
  
== The <tt>attribute</tt> Element ==
+
== The <code>attribute</code> Element ==
 
=== Make the page more readable ===
 
=== Make the page more readable ===
It defines a XML attribute of the enclosing element. The content of the element is the attribute value, while the <tt>name</tt> attribute specifies the attribute name. It is useful if the value of an attribute is sophisticated, or the attribute is conditional. With proper use, it makes the page more readable.  
+
It defines a XML attribute of the enclosing element. The content of the element is the attribute value, while the <code>name</code> attribute specifies the attribute name. It is useful if the value of an attribute is sophisticated, or the attribute is conditional. With proper use, it makes the page more readable.  
  
 
For example:
 
For example:
Line 151: Line 151:
 
</source>
 
</source>
  
In the following example, title of the window depends on <tt>${new}</tt>.
+
In the following example, title of the window depends on <code>${new}</code>.
 
<source lang="xml" >
 
<source lang="xml" >
 
  <window>
 
  <window>
Line 171: Line 171:
 
</source>
 
</source>
 
   
 
   
where <tt>ol</tt> and <tt>li</tt> are part of the native content. They are not ZK components. They will be eventually converted to a String instance and assigned to the specified attribute. If values has three elements, the above example is equivalent to the following:
+
where <code>ol</code> and <code>li</code> are part of the native content. They are not ZK components. They will be eventually converted to a String instance and assigned to the specified attribute. If values has three elements, the above example is equivalent to the following:
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 205: Line 205:
  
  
== The <tt>custom-attributes</tt> element ==
+
== The <code>custom-attributes</code> element ==
The <tt>custom-attributes</tt> element is used to defines a set of custom attributes. Custom attributes are objects associated with a particular scope. Acceptable scopes include component, ID space, page, desktop, session and application.
+
The <code>custom-attributes</code> element is used to defines a set of custom attributes. Custom attributes are objects associated with a particular scope. Acceptable scopes include component, ID space, page, desktop, session and application.
  
As depicted below, <tt>custom-attributes</tt> is convenient to assign custom attributes without programming.
+
As depicted below, <code>custom-attributes</code> is convenient to assign custom attributes without programming.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 245: Line 245:
 
</source>
 
</source>
 
   
 
   
=== Retrieve attributes directly in EL and <tt>zscript</tt> ===
+
=== Retrieve attributes directly in EL and <code>zscript</code> ===
  
 
Since ZK 5.0, the custom attribute can be accessed directly if its name is a valid Java name. For example,
 
Since ZK 5.0, the custom attribute can be accessed directly if its name is a valid Java name. For example,
Line 263: Line 263:
  
 
=== Retrieve attributes in EL via built-in variables ===
 
=== Retrieve attributes in EL via built-in variables ===
A custom attribute can be retrieved from one of the following built-in variables, <tt>componentScope</tt>, <tt>spaceScope</tt>, <tt>pageScope</tt>, <tt>desktopScope</tt>, <tt>sessionScope</tt>, and <tt>applicationScope</tt>. They are all maps representing different scopes.
+
A custom attribute can be retrieved from one of the following built-in variables, <code>componentScope</code>, <code>spaceScope</code>, <code>pageScope</code>, <code>desktopScope</code>, <code>sessionScope</code>, and <code>applicationScope</code>. They are all maps representing different scopes.
  
 
<source lang="javascript">
 
<source lang="javascript">
Line 269: Line 269:
 
</source>
 
</source>
  
Notice that EL expression is evaluated against the component being created. Sometime it is subtle to notice. For example, <tt>${componentScope.simple}</tt> is evaluated to <tt>null</tt>, in the following codes. Why? It is a shortcut of <tt><label value="${componentScope.simple}"/></tt>. In other words, the component, <tt>self</tt>, is the label rather than the window, when the EL is evaluated.
+
Notice that EL expression is evaluated against the component being created. Sometime it is subtle to notice. For example, <code>${componentScope.simple}</code> is evaluated to <code>null</code>, in the following codes. Why? It is a shortcut of <code><label value="${componentScope.simple}"/></code>. In other words, the component, <code>self</code>, is the label rather than the window, when the EL is evaluated.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 287: Line 287:
 
</source>
 
</source>
 
   
 
   
'''Tip:''' Don't confuse <tt><attribute></tt> with <tt><custom-attributes></tt>. They are irrelevant. The <tt>attribute</tt> element is a way to assign value to a predefined XML attribute of the enclosing element, while the <tt>custom-attributes</tt> element is used to assign new custom attributes to particular scopes.
+
'''Tip:''' Don't confuse <code><attribute></code> with <code><custom-attributes></code>. They are irrelevant. The <code>attribute</code> element is a way to assign value to a predefined XML attribute of the enclosing element, while the <code>custom-attributes</code> element is used to assign new custom attributes to particular scopes.
  
=== Specify a list or a map of values with the <tt>composite</tt> Attribute ===
+
=== Specify a list or a map of values with the <code>composite</code> Attribute ===
If you want to specify a list of values, you can specify the <tt>composite</tt> attribute with <tt>list</tt> as follows.
+
If you want to specify a list of values, you can specify the <code>composite</code> attribute with <code>list</code> as follows.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 298: Line 298:
 
Then, it is converted to a list with two elements. The first element is "apple" and the second "orange".
 
Then, it is converted to a list with two elements. The first element is "apple" and the second "orange".
  
If you want to specify a map of values, you can specify the <tt>composite</tt> attribute with <tt>map</tt> as follows.
+
If you want to specify a map of values, you can specify the <code>composite</code> attribute with <code>map</code> as follows.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 306: Line 306:
 
Then, it is converted to a map with two entries. The first entry is ("juice", "apple") and the second ("flavor", "orange").
 
Then, it is converted to a map with two entries. The first entry is ("juice", "apple") and the second ("flavor", "orange").
  
=== Specify <tt>null</tt> ===
+
=== Specify <code>null</code> ===
In the following example, <tt>var </tt>is an empty string.
+
In the following example, <code>var </code>is an empty string.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 331: Line 331:
 
|-
 
|-
 
|  composite
 
|  composite
| [Optional][Default: <tt>none</tt>]
+
| [Optional][Default: <code>none</code>]
  
Specifies the format of the value. It could be <tt>none</tt>, <tt>list</tt> or <tt>map</tt>.
+
Specifies the format of the value. It could be <code>none</code>, <code>list</code> or <code>map</code>.
  
 
|}
 
|}
  
== The <tt>variables</tt> element ==
+
== The <code>variables</code> element ==
 
  [deprecated since 5.0] The concept of namespace (and variables) is deprecated and replaced with the custom attribute ([[#The custom-attributes element]]).
 
  [deprecated since 5.0] The concept of namespace (and variables) is deprecated and replaced with the custom attribute ([[#The custom-attributes element]]).
  
The <tt>variables</tt> element is used to define a set of variables in the namespace. It is equivalent to the <tt>setVariable</tt> method of <tt>Component</tt>.
+
The <code>variables</code> element is used to define a set of variables in the namespace. It is equivalent to the <code>setVariable</code> method of <code>Component</code>.
  
 
  A namespace is a unique scope that is associated with an ID space. The relation is one-to-one.
 
  A namespace is a unique scope that is associated with an ID space. The relation is one-to-one.
 
  However, it is deprecated since ZK 5.0 (because of the redundancy with custom attributes).
 
  However, it is deprecated since ZK 5.0 (because of the redundancy with custom attributes).
  
As depicted below, <tt>variables</tt> is convenient to assign variables without programming.
+
As depicted below, <code>variables</code> is convenient to assign variables without programming.
  
 
In the following example, we set a variable named "rich", and its value is "simple", a variable named "simple" and its value is "intuitive".
 
In the following example, we set a variable named "rich", and its value is "simple", a variable named "simple" and its value is "intuitive".
Line 377: Line 377:
 
</source>
 
</source>
  
The result will show <tt>1: Test 2:</tt>  . Since <tt>title</tt> is invisible in <tt>win1</tt>. Because it is stored in namespace, and <tt>window</tt> is an id space, and therefore a namespace.
+
The result will show <code>1: Test 2:</code>  . Since <code>title</code> is invisible in <code>win1</code>. Because it is stored in namespace, and <code>window</code> is an id space, and therefore a namespace.
 
   
 
   
Like <tt>Component</tt>'s <tt>setVariable</tt>, you can control whether to declare variables local to the current ID space as follows. If not specified, <tt>local="false"</tt> is assumed.
+
Like <code>Component</code>'s <code>setVariable</code>, you can control whether to declare variables local to the current ID space as follows. If not specified, <code>local="false"</code> is assumed.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 385: Line 385:
 
</source>
 
</source>
 
   
 
   
'''Note''': local is an attribute of <tt>variables</tt>, so it's not parsed as a variable name. By default, <tt>local</tt> is false. It means if parent namespace has same variable, its value will be changed too.
+
'''Note''': local is an attribute of <code>variables</code>, so it's not parsed as a variable name. By default, <code>local</code> is false. It means if parent namespace has same variable, its value will be changed too.
  
In the following example, you can change the value of <tt>local</tt> to see the difference.
+
In the following example, you can change the value of <code>local</code> to see the difference.
 
<source lang="xml" >
 
<source lang="xml" >
 
<window>
 
<window>
Line 399: Line 399:
 
</source>
 
</source>
  
=== Specify a list or a map of values with the <tt>composite</tt> Attribute ===
+
=== Specify a list or a map of values with the <code>composite</code> Attribute ===
  
If you want to specify a list of values, you can specify the <tt>composite</tt> attribute with <tt>list</tt> as follows.
+
If you want to specify a list of values, you can specify the <code>composite</code> attribute with <code>list</code> as follows.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 409: Line 409:
 
Then, it is converted to a list with two elements. The first element is "apple" and the second "orange".
 
Then, it is converted to a list with two elements. The first element is "apple" and the second "orange".
  
If you want to specify a map of values, you can specify the <tt>composite</tt> attribute with <tt>map</tt> as follows.
+
If you want to specify a map of values, you can specify the <code>composite</code> attribute with <code>map</code> as follows.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 417: Line 417:
 
Then, it is converted to a map with two entries. The first entry is ("juice", "apple") and the second ("flavor", "orange").
 
Then, it is converted to a map with two entries. The first entry is ("juice", "apple") and the second ("flavor", "orange").
  
=== Specify <tt>null</tt> ===
+
=== Specify <code>null</code> ===
In the following example, <tt>var </tt>is an empty string.
+
In the following example, <code>var </code>is an empty string.
  
 
<source lang="xml" >
 
<source lang="xml" >
Line 436: Line 436:
 
== Quiz ==
 
== Quiz ==
 
#What's root component? How many root component can a page have?
 
#What's root component? How many root component can a page have?
#Where does <tt>setVariable</tt> store variable?
+
#Where does <code>setVariable</code> store variable?
#Where does <tt>custom-attributes</tt> store variable?
+
#Where does <code>custom-attributes</code> store variable?
  
 
{{ ZKDevelopersGuidePageFooter}}
 
{{ ZKDevelopersGuidePageFooter}}

Latest revision as of 13:28, 19 January 2022

DocumentationUML ZK Elements
UML ZK Elements


Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


Overview

ZK elements are used to control ZUML pages other than creating components.

The zk Element

 <zk>...</zk>

It is a special element used to aggregate other components. Unlike a real component (say, hbox or div), it is not part of the component tree being created. In other words, it doesn't represent any component. For example,

 <window>
     <zk>
         <textbox/>
         <textbox/>
     </zk>
 </window>

is equivalent to

 <window>
     <textbox/>
     <textbox/>
 </window>

Then, what is it used for?

Multiple Root Elements in a Page

Due to XML's syntax limitation, we can only specify one document root. Thus, if you have multiple root components, you must use zk as the document root to group these root components.[1]

 <?page title="Multiple Root"?>
 <zk>
     <window title="First">
     ...
     </window>
     <window title="Second">
     ...
     </window>
 </zk>

Iteration Over Versatile Components

The zk element, like components, supports the forEach attribute. Thus, you could use it to generate different type of components depending on the conditions. In the following example, we assume mycols is a collection of objects that have member, isUseText(). For each object in mycols, a textbox will be created if isUseText() returns true. useText is auto transformed to isUseText() by EL expression evaluation.

 <window>
     <zk forEach="${mycols}">
         <textbox if="${each.useText}"/>
     </zk>
 </window>

For more information about forEach, if, please refer to section The ZK Attributes.

Notes

  1. A component without any parent is called a root component

The zscript Element

Write java code in ZUML

<zscript>Scripting codes</zscript>
<zscript src="uri"/>

It defines a piece of the scripting codes, say the Java codes, that will be interpreted when the page is evaluated. The language of the scripting codes is, by default, Java.

The zscript element has two formats as shown above. The first format is used to embed the scripting codes directly in the page. The second format is used to reference an external file that contains the scripting codes.

You can also embed zscript in event handler in ZUML. In the following example,

 <button onClick="alert(&quot;Hi&quot;)"/>

alert("quot;Hi"quot;)is also a piece of zscript code.

Attribute Name
Description
src [Optional][Default: none]

Specifies the URI of the file containing the scripting codes. If specified, the scripting codes will be loaded as if they are embedded directly.


deferred [Optional][Default: false]

Whether to defer the evaluation of this element until the first non-deferred zscript codes of the same language need to be evaluated. Refer to the How to Defer the Evaluation section below.

How to Defer the Evaluation

ZK loads the interpreter before it is going to evaluate the first zscript codes. For example, the Java interpreter is loaded when the user clicks the button in the following example.

 <button onClick="alert(&quot;Hi&quot;)"/>

On the other hand, the interpreter is loaded when loading the following ZUML page, since the zscript element needs to be evaluated when loading the page.

 <window>
     <zscript>
     void add() {
     }
     </zscript>
     <button onClick="add()"/>
 </window>

If you prefer to defer the loading of the interpreter, you can specify the deferred option with true. Then, the interpreter won't be loaded, until the user clicks the button.

 <window>
     <zscript deferred="true">
     void add() {
     }
     </zscript>
     <button onClick="add()"/>
 </window>

Note: The evaluation of EL expressions specified in the if, unless and src attributes are also deferred.

Note: If the component is detached from the page by the time the interpreter is loaded, the zscript codes are ignored. For example, if the window in the previous example no longer belongs to the page, the deferred zscript won't be interpreted.

The attribute Element

Make the page more readable

It defines a XML attribute of the enclosing element. The content of the element is the attribute value, while the name attribute specifies the attribute name. It is useful if the value of an attribute is sophisticated, or the attribute is conditional. With proper use, it makes the page more readable.

For example:

<button label="Say Hello" onClick="alert(&quot;Hello World!&quot;)"/>

is equivalent to

<button label="Say Hello">
     <attribute name="onClick">alert("Hello World!");</attribute>    
</button>

In the following example, title of the window depends on ${new}.

 <window>
     <attribute name="title" if="${new}">New is True</attribute>
 </window>

Native Content

In addition, you can specify a XML fragment as the value of the attribute. The XML fragment is so-called the native content.

 <html>
     <attribute name="content">
         <ol>
             <li forEach="${values}">${each}</li>
         </ol>
     </attribute>
 </html>

where ol and li are part of the native content. They are not ZK components. They will be eventually converted to a String instance and assigned to the specified attribute. If values has three elements, the above example is equivalent to the following:

 <html>
     <attribute name="content">
         <ol>
             <li>${values[0]}</li>
             <li>${values[1]}</li>
             <li>${values[2]}</li>
         </ol>
     </attribute>
 </html>
Attribute Name
Description
name [Required]

Specifies the attribute name.

trim [Optional][Default: false]

Specifies whether to omit the leading and trailing white-spaces of the attribute value.


The custom-attributes element

The custom-attributes element is used to defines a set of custom attributes. Custom attributes are objects associated with a particular scope. Acceptable scopes include component, ID space, page, desktop, session and application.

As depicted below, custom-attributes is convenient to assign custom attributes without programming.

 <window>
     <custom-attributes main.rich="simple" very-simple="intuitive"/>
 </window>

It is equivalent to

 <window>
     <zscript>
         self.setAttribute("main.rich", "simple");
         self.setAttribute("very-simple", "intuitive");
     </zscript>
 </window>

Moreover, you could specify what scope to assign the custom attributes to.

 <window id="main" title="Welcome">
     <custom-attributes scope="desktop" shared="${main.title}"/>
 </window>

It is equivalent to

 <window id="main">
     <zscript>
         desktop.setAttribute("shared", main.title);
     </zscript>
 </window>

Retrieve attributes directly in EL and zscript

Since ZK 5.0, the custom attribute can be accessed directly if its name is a valid Java name. For example,

<window>
  <custom-attributes less="is more"/>
  ${less}
  <zscript>
  if ("is more".equals(less)) { //true
  }
  </zscript>
</window>

ZK will look up the value of a custom attribute from the nearest component, to its parent, to its ancestor, to its page, to its desktop, to the session, and to the application, until found.

Retrieve attributes in EL via built-in variables

A custom attribute can be retrieved from one of the following built-in variables, componentScope, spaceScope, pageScope, desktopScope, sessionScope, and applicationScope. They are all maps representing different scopes.

${pageScope.something}

Notice that EL expression is evaluated against the component being created. Sometime it is subtle to notice. For example, ${componentScope.simple} is evaluated to null, in the following codes. Why? It is a shortcut of <label value="${componentScope.simple}"/>. In other words, the component, self, is the label rather than the window, when the EL is evaluated.

 <window>
     <custom-attributes simple="intuitive"/>
     ${componentScope.simple}
 </window>

is equivalent to

 <window>
     <custom-attributes simple="intuitive"/>
     <label value="${componentScope.simple}"/><!-- self is label not window -->
 </window>

Tip: Don't confuse <attribute> with <custom-attributes>. They are irrelevant. The attribute element is a way to assign value to a predefined XML attribute of the enclosing element, while the custom-attributes element is used to assign new custom attributes to particular scopes.

Specify a list or a map of values with the composite Attribute

If you want to specify a list of values, you can specify the composite attribute with list as follows.

 <custom-attributes simple="apple, orange" composite="list"/>

Then, it is converted to a list with two elements. The first element is "apple" and the second "orange".

If you want to specify a map of values, you can specify the composite attribute with map as follows.

 <custom-attributes simple="juice=apple, flavor=orange" composite="map"/>

Then, it is converted to a map with two entries. The first entry is ("juice", "apple") and the second ("flavor", "orange").

Specify null

In the following example, var is an empty string.

 <custom-attributes var=""/>

To define a variable with the null value, use the following statement.

 <custom-attributes var="${null}"/>
Attribute Name
Description
scope [Optional][Default: component]

Specifies what scope to associate the custom attributes to.

composite [Optional][Default: none]

Specifies the format of the value. It could be none, list or map.

The variables element

[deprecated since 5.0] The concept of namespace (and variables) is deprecated and replaced with the custom attribute (#The custom-attributes element).

The variables element is used to define a set of variables in the namespace. It is equivalent to the setVariable method of Component.

A namespace is a unique scope that is associated with an ID space. The relation is one-to-one.
However, it is deprecated since ZK 5.0 (because of the redundancy with custom attributes).

As depicted below, variables is convenient to assign variables without programming.

In the following example, we set a variable named "rich", and its value is "simple", a variable named "simple" and its value is "intuitive".

 <window>
     <variables rich="simple" simple="intuitive"/>
 </window>

It is equivalent to

 <window>
     <zscript>
         self.setVariable("rich", "simple", false);
         self.setVariable("simple", "intuitive", false);
     </zscript>
 </window>

Of course, you can specify EL expressions for the values.

 <window id="win1">
     <window id="w" title="Test">
	 <variables title="${w.title}"/>
         1: ${title}
     </window>
     2: ${title}
 </window>

The result will show 1: Test 2: . Since title is invisible in win1. Because it is stored in namespace, and window is an id space, and therefore a namespace.

Like Component's setVariable, you can control whether to declare variables local to the current ID space as follows. If not specified, local="false" is assumed.

 <variables simple="rich" local="true"/>

Note: local is an attribute of variables, so it's not parsed as a variable name. By default, local is false. It means if parent namespace has same variable, its value will be changed too.

In the following example, you can change the value of local to see the difference.

<window>
	<variables title="def"/>
	<window title="Test">
		<variables title="abc" local="false" />	
		1: ${title}
	</window>
	2: ${title}
</window>

Specify a list or a map of values with the composite Attribute

If you want to specify a list of values, you can specify the composite attribute with list as follows.

 <variables simple="apple, orange" composite="list"/>

Then, it is converted to a list with two elements. The first element is "apple" and the second "orange".

If you want to specify a map of values, you can specify the composite attribute with map as follows.

 <variables simple="juice=apple, flavor=orange" composite="map"/>

Then, it is converted to a map with two entries. The first entry is ("juice", "apple") and the second ("flavor", "orange").

Specify null

In the following example, var is an empty string.

 <variables var=""/>

To define a variable with the null value, use the following statement.

 <variables var="${null}"/>

See Also

From ZK Forum

Quiz

  1. What's root component? How many root component can a page have?
  2. Where does setVariable store variable?
  3. Where does custom-attributes store variable?



Last Update : 2022/01/19

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