Search code examples
zk

How to create a Zul template of a button and then apply it


The example provided in the zkessentials book is too simplistic yet the documentation goes way overboard (in my opinion).

What I'd like to do is create a template with just a button and pass it it's style class and it's label value.

The example in the book shows a ten-line Zul and then says you have to declare its a template but does not mention where nor does it mention how to pass values to the template.

The other examples introduce @{define(left)} and @{insert(left)} which obviously with no explanation makes it pretty difficult to make it out if I can just pass values there or if it's doing some matching.

Thanks for your help.


Solution

  • Maybe you just want to make a predefined class who extends button.

    public class MyButton extends Button implements BeforeCompose {
    
        @Override
        public void beforeCompose() {
            setLabel("new Button");
            setStyle("...");
            // and more settings you want to set as default
        }
    }
    

    Now you have this class you can use it in 3 different ways. First of all :

    <button use="my.path.MyButton"/>
    

    Second :

    <?component name="mybutton" class="my.path.MyButton"?>
    <mybutton />
    

    Thirth :

    Add this in lang-addon.xml :

    <component>
        <component-name>mybutton</component-name>
        <extends>button</extends>
        <component-class>my.path.MyButton</component-class>
    </component>
    

    and usage in zul :

    <mybutton />