Inline Macros"

From Documentation
Line 48: Line 48:
 
Notice that all properties, including <tt>id</tt>, are passed to the inline macro too.
 
Notice that all properties, including <tt>id</tt>, are passed to the inline macro too.
  
= Regular Macro =
+
= Inline versus Regular Macro =
On the other hand, ZK will create a real component (an instance of <javadoc>org.zkoss.zk.ui.HtmlMacroComponent</javadoc>) to represent the regular macro. That is, the macro component is created as the parent of the components that are defined in the macro.
 
  
Inline macros are easier to integrate into sophisticated pages. For example, you ''cannot'' use ''regular'' components in the previous example since <tt>rows</tt> accepts only <tt>row</tt>, not macro components. It is easier to access to all components defined in a macro since they are in the same ID space. It also means the developers must be aware of the implementation to avoid name conflicts.
+
As described above, an inline macro is expanded inline when it is used as if they are entered directly. On the other hand, ZK will create a component (an instance of <javadoc>org.zkoss.zk.ui.HtmlMacroComponent</javadoc> or deriving) to represent the regular macro. That is, the macro component is created as the parent of the components that are defined in the template.
  
Regular macros allow the component developers to provide additional API and hide the implementation from the component users. Each regular macro component is an ID space owner, so there is no name conflicts. The users of regular macros usually assume nothing about the implementation. Rather, they access via the well-defined API.
+
Inline macros are easier to integrate into sophisticated pages. For example, you ''cannot'' use ''regular'' components in the previous example since <tt>rows</tt> accepts only <tt>row</tt> as children, not macro components. It is also easier to access to all components defined in a macro since they are expanded inline. However, it also means the developers must take care of <code>id</code> by themselves.
 +
 
 +
On the other hand, regular macros allow the component developers to provide a custom Java class to represent the component, such that a better abstraction and addition functionality can be implemented. We will discuss it more in the [[Macro Component/Implement Custom Java Class|following section]].
  
 
= An Example =
 
= An Example =

Revision as of 09:05, 8 November 2010

An inline macro is a special macro component that behaves like inline-expansion. Unlike a regular macro component, ZK doesn't create a macro component. Rather, it inline-expands the components defined in the macro URI, as if the content of the in-line macro's template is entered directly in the target page.

Declare Inline Macro

To declare an inline macro, we have to specify inline="true" in the component directive, while the definition and the use of an inline macro is the same as the regular macro components (i.e., non-inline).

For example, suppose we have a macro definition (aka., template) as follows:

<!-- username.zul: (macro definition) -->
<row>
	Username
	<textbox id="${arg.id}" value="${arg.name}"/>
</row>


We can declare it as an line macro as follows:

<!-- target page -->
<?component name="username" inline="true" macroURI="username.zul"?>
<grid>
	<rows>
		<username id="ua" name="John"/>
	</rows>
</grid>


Then, it is equivalent to:

<grid>
	<rows>
		<row>
			Username
			<textbox id="ua" value="John"/>
		</row>
	</rows>
</grid>

Notice that all properties, including id, are passed to the inline macro too.

Inline versus Regular Macro

As described above, an inline macro is expanded inline when it is used as if they are entered directly. On the other hand, ZK will create a component (an instance of HtmlMacroComponent or deriving) to represent the regular macro. That is, the macro component is created as the parent of the components that are defined in the template.

Inline macros are easier to integrate into sophisticated pages. For example, you cannot use regular components in the previous example since rows accepts only row as children, not macro components. It is also easier to access to all components defined in a macro since they are expanded inline. However, it also means the developers must take care of id by themselves.

On the other hand, regular macros allow the component developers to provide a custom Java class to represent the component, such that a better abstraction and addition functionality can be implemented. We will discuss it more in the following section.

An Example

inline.zul: (the macro definition)

<row>
	<textbox value="${arg.col1}"/>
	<textbox value="${arg.col2}"/>
</row>

useinline.zul: (the target page)

<?component    name="myrow"    macroURI="inline.zul"  inline="true"?>
<window    title="Test of inline macros"    border="normal">
	<zscript><![CDATA[
		import    org.zkoss.util.Pair;
		List  infos = new LinkedList();
		for(int j     = 0;j    <10;++j){
			infos.add(new Pair("A" + j, "B" +j));
		}
	]]>
	</zscript>
	<grid>
		<rows>
			<myrow    col1="${each.x}"    col2="${each.y}"    forEach="${infos}"/>
		</rows>
	</grid>
</window>

Version History

Last Update : 2010/11/8

Version Date Content
     



Last Update : 2010/11/08

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