Class HtmlTag

  • All Implemented Interfaces:
    AutoCloseable

    public final class HtmlTag
    extends Object
    implements AutoCloseable
    Tiny DSL for generating HTML. Usage:
    
     try ( HtmlTag.Html html = HtmlTag.html( outputPath ) ) {
         html.head( HtmlTag.head("title", "My Page") );
         try ( HtmlTag body = html.body() ) {
             body.tag("h1")                  // opens a new <h1> tag
                 .text("Welcome to My Page") // adds text content to the tag
                 .close();                   // closes the </h1> tag
             body.text("This is a very neat page.");
             body.p();
             body.text("You should come back when there is more content.");
             body.br();
             body.text("Until then, here is a picture of a cat for you to look at:");
             // img tags should not be closed, so we simply don't invoke the close() method.
             body.tag("img", HtmlTag.attr("src", "http://thecatapi.com/api/images/get?format=src&type=gif") );
             body.p();
             body.textTag("b", "To do:");
             try ( HtmlTag list = body.tag("ul") ) {
                 list.textTag("li", "Find cuter cat")
                     .textTag("li", "???")
                     .textTag("li", "Profit!");
             }
         }
     }
     
    • Method Detail

      • p

        public void p()
      • br

        public void br()
      • attr

        public static HtmlTag.Attribute attr​(String attribute,
                                             String value)
        Generate html tag attributes for use in . This is an alternative to body.tag( "img", src -> imgUri ), allowing the use of the API on earlier builds of the JDK as body.tag( "img", attr( "src", imgUri ) ).
        Parameters:
        attribute - the name of the attribute.
        value - the value of the attribute.
        Returns:
        an object that generates the attribute.
      • meta

        public static HtmlTag.HeadTag meta​(String name,
                                           String value)
        Generate meta tags for use in <head>.
        Parameters:
        name - the name of the meta attribute.
        value - the value of the meta attribute.
        Returns:
        an object that generates the tag in the head.
      • head

        public static HtmlTag.HeadTag head​(String tag,
                                           String text,
                                           HtmlTag.Attribute... attributes)
        Generate html tags for use in <head>. Allows adding attributes to a head tag.
        Parameters:
        tag - the name of the head tag.
        text - the contents of the head tag.
        attributes - the attributes of the head tag.
        Returns:
        an object that generates the tag in the head.
      • output

        public Output output()
        Unsafe access to the underlying Output. This provides direct access to the underlying Output and should be used carefully, since its use might result in invalid HTML.
        Returns:
        the underlying Output