Skip to content

4. How to create custom attribute with and without value

Web Firm Framework edited this page Aug 19, 2017 · 2 revisions

You can use com.webfirmframework.wffweb.tag.html.attributewff.CustomAttribute class to create a custom attribute.

Custom attribute with value :

Div div = new Div(null, new CustomAttribute("attribute-with", "value"));
System.out.println(div);

prints

<div attribute-with="value"></div>

Custom attribute without value :

Div div = new Div(null, new CustomAttribute("attribute-without-value"));
System.out.println(div);

prints

<div attribute-without-value></div>

OR

You can also keep your own implementation class by extending CustomAttribute class, see the below samples

To create attribute with value :-

import com.webfirmframework.wffweb.tag.html.attributewff.CustomAttribute;

public class CommonDataAttribute extends CustomAttribute {

    private static final long serialVersionUID = 1L;

    public CommonDataAttribute(String attributeValue) {
        super("common-data", attributeValue);
    }

    // TODO write the feature methods for this attribute

}
public static void main(String[] args) {

    Div div = new Div(null, new CommonDataAttribute("data1")) {
        Div div = new Div(this, new CommonDataAttribute("data2"));
    };

    System.out.println(div.toHtmlString(true));
}

this will print

<div common-data="data1">
    <div common-data="data2"></div>
</div>

To create attribute without value :-

import com.webfirmframework.wffweb.tag.html.attributewff.CustomAttribute;

public class CommonDataNoValueAttribute extends CustomAttribute {

    private static final long serialVersionUID = 1L;

    public CommonDataNoValueAttribute() {
        super("common-data-no-val");
    }
    // TODO write the feature methods for this attribute

}
public static void main(String[] args) {

    Div div = new Div(null, new CommonDataNoValueAttribute()) {
        Div div = new Div(this, new CommonDataNoValueAttribute(), new Id("55555Id"));
    };

    System.out.println(div.toHtmlString(true));
}

this will print

<div common-data-no-val>
    <div common-data-no-val id="55555Id"></div>
</div>