ZK Elements"

From Documentation
m (Created page with '{{ZKDevelopersGuidePageHeader}} __TOC__ Helper component that's not UI. It helps to save variable, write java code in ZUML, and others. {| border="1" ! <center>Element Name</c…')
 
m (correct highlight (via JWB))
 
(One intermediate revision by one other user not shown)
Line 9: Line 9:
 
! <center>Description</center>
 
! <center>Description</center>
 
|-
 
|-
|  [[ZUML_ZK Elements#The_zk_Element|<tt>zk</tt>]]
+
|  [http://books.zkoss.org/wiki/ZK_Developer%27s_Guide/Fundamental_ZK/ZK_User_Interface_Markup_Language/ZK_Elements#The_zk_Element<code>zk</code>]
 
| Aggregate other components
 
| Aggregate other components
 
|-
 
|-
| [[ZUML_ZK Elements#The_zscript_Element|<tt>zscript</tt>]]
+
| [http://books.zkoss.org/wiki/ZK_Developer%27s_Guide/Fundamental_ZK/ZK_User_Interface_Markup_Language/ZK_Elements#The_zscript_Element<code>zscript</code>]
 
| Write java code in ZUML
 
| Write java code in ZUML
 
|-
 
|-
| [[ZUML_ZK Elements#The_attribute_Element|<tt>attribute</tt>]]
+
| [http://books.zkoss.org/wiki/ZK_Developer%27s_Guide/Fundamental_ZK/ZK_User_Interface_Markup_Language/ZK_Elements#The_attribute_Element<code>attribute</code>]]
 
| Make the page more readable  
 
| Make the page more readable  
 
|-
 
|-
|  [[ZUML_ZK Elements#The_variables_element|<tt>variables</tt>]]
+
|  [http://books.zkoss.org/wiki/ZK_Developer%27s_Guide/Fundamental_ZK/ZK_User_Interface_Markup_Language/ZK_Elements#The_variables_element<code>variables</code>]
 
| Store variable in namespace scope  
 
| Store variable in namespace scope  
 
|-
 
|-
| [[ZUML_ZK Elements#The_custom-attributes_element|<tt>custom-attributes</tt>]]
+
| [http://books.zkoss.org/wiki/ZK_Developer%27s_Guide/Fundamental_ZK/ZK_User_Interface_Markup_Language/ZK_Elements#The_custom-attributes_element<code>custom-attributes</code>]
 
| Store variables in different scopes
 
| Store variables in different scopes
 
|}
 
|}
Line 28: Line 28:
 
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 34: Line 34:
 
</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 57: Line 57:
  
 
=== 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 72: Line 72:
  
 
=== 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 82: Line 82:
 
</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 94: Line 94:
 
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 116: Line 116:
 
|-
 
|-
 
|  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 129: Line 129:
 
</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 141: Line 141:
 
</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 153: Line 153:
 
</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 175: Line 175:
 
</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 195: Line 195:
 
</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 229: Line 229:
  
  
== 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 269: Line 269:
 
</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 287: Line 287:
  
 
=== 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 293: Line 293:
 
</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 311: Line 311:
 
</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 322: Line 322:
 
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 330: Line 330:
 
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 355: Line 355:
 
|-
 
|-
 
|  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 401: Line 401:
 
</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 409: Line 409:
 
</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 423: Line 423:
 
</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 433: Line 433:
 
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 441: Line 441:
 
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 460: Line 460:
 
== 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 10:37, 19 January 2022

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


Helper component that's not UI. It helps to save variable, write java code in ZUML, and others.

Element Name
Description
zk Aggregate other components
zscript Write java code in ZUML
attribute] Make the page more readable
variables Store variable in namespace scope
custom-attributes Store variables in different scopes

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.