Three Steps to Use Macro Components

From Documentation
Three Steps to Use Macro Components

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

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

It takes three steps to use macro components as follows.

  1. Implements a macro component by a ZUML page.
  2. Declare the macro component in the page that is going to use it.
  3. Use the macro components, which is no difference that other components.

Tip: In addition to define a macro component in page, you can put its definition into a language addon such all pages are able to access the macro component.

Step 1. The Implementation

All you need to do is to prepare a ZUML page that describes what the component consists of. In other words, the page is a template of the macro.

For example, assume we want to pack a label and a text box as a macro component. Then we could create page, say /WEB-INF/macros/username.zul, as follows.

	Username: <textbox/>

It is done!

The ZUML page implementing a macro component is the same as any other pages, so any ZUML page can be used as a macro component.

Step 2. The Declaration

Before instantiating a macro component, you have to declare first. One of simplest way to declare is to use the component directives.

<?component name="username" macroURI="/WEB-INF/macros/username.zul"?>

As shown, you have to declare the name (the name attribute) and the URI of the page (the macroURI attribute).

Other Properties

In additions to the name, macroURI and class[1] attributes, you can specify a list of initial properties that will be used to initialize a component when it is instantiated.

<?component name="mycomp" macroURI="/macros/mycomp.zul"
   myprop="myval" another="anotherval"?>



is equivalent to

<mycomp myprop="myval1" another="anotherval"/>
  1. The class attribute will be discussed later.

Step 3. The Use

The use of a macro component is no different than others.


Pass Properties

Like an ordinary component, you can specify properties (a.k.a., attributes) when using a macro component as follows.

<?component name="username" macroURI="/WEB-INF/macros/username.zul"?>
	<username who="John"/>

All these properties specified are stored in a map that is then passed to the template via a variable called arg. Then, in the template, you could access these properties as follows.

	Username: <textbox value="${arg.who}"/>

Note: arg is available only when rendering the macro page. To access in the event listener, you have to use getDynamicProperty instead. Refer to the Provide Additional Methods section for more details.


In additions to the specified properties (a.k.a., attributes), a property called arg.includer is always passed to represent the parent of the components defined in a macro template.

If a regular macro is created, arg.includer is the macro component itself. If an inline macro is created, arg.includer is the parent component, if any. Refer to the Inline Macros section for more information.

In the above example, arg.includer represents the regular macro component, <username who="John"/>, and is the parent of <hbox> (defined in username.zul).

Last Update : 2022/01/19

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