Hella easy HTML in Java
hella-html is a library that makes it hella easy to generate dynamic HTML in vanilla Java.
- Very lightweight and fast, the primary logic is 10 lines of O(n) code
- Requires no external dependencies
- Supports all standard (and custom) HTML tags
- Released under the MIT license and free for everyone
Getting started
Add the Maven dependency.
<dependency>
<groupId>sh.hellagroupId>
<artifactId>hella-htmlartifactId>
<version>1.0.1version>
dependency>
or for Gradle users...
compile "sh.hella:hella-html:1.0.1"
And finally the static import.
import static sh.hella.html.Html.*;
Example usage
Java code
html(
head(
title(text("Hello World"))
),
body(
div(clazz("container"),
h1(text("Hello world, HTML edition!")),
p(text("Made hella easy in Java")),
a(href("https://hella.sh/"), text("Visit our homepage!"))
)
)
).toString();
HTML output ">
> <html> <head> <title>Hello Worldtitle> head> <body> <div class="container"> <h1>Hello world, HTML edition!h1> <p>Made hella easy in Javap> <a href="https://www.hella.sh/">Visit our homepage!a> div> body> html>
Note: formatted for readability, generated HTML will not have newlines or tabs.
Add and remove tags dynamically
Add and remove tags with the add
and remove
methods. Use them conditionally, inside loops, or however else you want.
Element contentDiv = div(clazz("content"));
Element textSpan = span(text("Dynamic text!"));
contentDiv.add(textSpan);
contentDiv.toString(); //
Dynamic text!
contentDiv.remove(textSpan);
contentDiv.toString(); //
Custom tags and attributes
Add custom tags with tag(name, children...)
and custom attributes with attr(name, value)
.
Java code
tag("myCustomTag",
div(attr("foo", "bar"),
text("Hello, custom tags and attributes!")
)
)
HTML output
<myCustomTag><div foo="bar">Hello, custom tags and attributes!div>myCustomTag>
Inlining content
Inline data directly from files with fromFile(path)
and from resources with fromResource(path)
.
Java code
style(fromResource("style.css")))
HTML output
<style>
(contents of style.css)
style>