tipsy / j2html

Java to HTML generator. Enjoy typesafe HTML generation.
https://j2html.com/
Apache License 2.0
765 stars 136 forks source link

Question: New html-tags #200

Closed eurostar77 closed 3 years ago

eurostar77 commented 3 years ago

Hi, I can not find html "picture" tag in j2html. How can I write custom tags?

sembler commented 3 years ago

The simplest way is to create a ContainerTag with the appropriate tag name:

new ContainerTag<>("picture");

If you will be creating a picture tag in multiple places you can create static factory methods like j2html provides (and we will add this one in the near future):

public static ContainerTag<?> picture(DomContent... dc){ return new ContainerTag<>("picture").with(dc); }

Or you can create a complete class along with the factory method(s):

public static final class PictureTag extends ContainerTag<PictureTag> {
    public PictureTag() {
        super("picture");
    }
}

public static PictureTag picture(DomContent... dc){
    return new PictureTag().with(dc);
}

...

html(
    body(
        picture(
            source(),
            img()
        )
    )
);
eurostar77 commented 3 years ago

Thanks for your help!