public final class HtmlTag extends Object implements AutoCloseable
try ( HtmlTag.Html html = HtmlTag.html( outputPath ) ) {
html.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:");
body.tag("img", src -> "http://thecatapi.com/api/images/get?format=src&type=gif");
body.p();
body.tag("b").text("To do:").close();
try ( HtmlTag list = body.tag("ul") ) {
list.tag("li").text("Find cuter cat").close();
list.tag("li").text("???").close();
list.tag("li").text("Profit!").close();
}
}
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
HtmlTag.Attribute<T> |
static class |
HtmlTag.Html |
| Modifier and Type | Method and Description |
|---|---|
static HtmlTag.Attribute<Void> |
attr(String attribute,
String value)
Generate html tag attributes for use in .
|
void |
br() |
void |
close() |
static HtmlTag.Attribute<HtmlTag> |
head(HtmlTag.Attribute<HtmlTag> tag,
HtmlTag.Attribute<Void>... attributes)
Generate html tags for use in
<head>. |
static HtmlTag.Attribute<HtmlTag> |
head(String tag,
Consumer<HtmlTag> content,
HtmlTag.Attribute<Void>... attributes)
Generate html tags for use in
<head>. |
static HtmlTag.Html |
html(Output output) |
static HtmlTag.Html |
html(Path file) |
static HtmlTag.Attribute<HtmlTag> |
meta(String name,
String value)
Generate meta tags for use in
<head>. |
Output |
output()
Unsafe access to the underlying
Output. |
void |
p() |
HtmlTag |
tag(String tag,
HtmlTag.Attribute<Void>... attributes) |
HtmlTag |
text(String text) |
public static HtmlTag.Html html(Path file)
public static HtmlTag.Html html(Output output)
@SafeVarargs public final HtmlTag tag(String tag, HtmlTag.Attribute<Void>... attributes)
public void p()
public void br()
public static HtmlTag.Attribute<Void> attr(String attribute, String value)
body.tag( "img", src -> imgUri ), allowing the use of the API on earlier
builds of the JDK as body.tag( "img", attr( "src", imgUri ) ).attribute - the name of the attribute.value - the value of the attribute.public static HtmlTag.Attribute<HtmlTag> meta(String name, String value)
<head>.name - the name of the meta attribute.value - the value of the meta attribute.public static HtmlTag.Attribute<HtmlTag> head(HtmlTag.Attribute<HtmlTag> tag, HtmlTag.Attribute<Void>... attributes)
<head>.
Allows adding attributes to a head tag.tag - the name and contents of the head tag.attributes - the attributes of the head tag.@SafeVarargs public static HtmlTag.Attribute<HtmlTag> head(String tag, Consumer<HtmlTag> content, HtmlTag.Attribute<Void>... attributes)
<head>.
This is an alternative to html.head( title -> "my title" ), allowing the use of the API on earlier
builds of the JDK as html.head( head( "title", title -> title.text( "my title" ) ) ).tag - the name of the head tag.content - generator for the content of the head tag.attributes - the attributes of the head tag.public void close()
close in interface AutoCloseableCopyright © 2019. All rights reserved.