DSL for writing HTML in Java
Java
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
README.md
pom.xml

README.md

java-html-dsl

DSL for writing html in Java http://benjiweber.co.uk/blog/2015/08/21/html-in-java

This

String doc =
    html(
        head(
            meta(charset -> "utf-8"),
            link(rel->stylesheet, type->css, href->"/my.css"),
            script(type->javascript, src -> "/some.js")
        ),
        body(
            h1("Hello World", style->"font-size:200%;"),
            article(
                p("Here is an interesting paragraph"),
                p(
                    "And another",
                    small("small")
                ),
                ul(
                        li("An"),
                        li("unordered"),
                        li("list")
                )
            )
        )
    ).asString();

Generates

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Java (vers. 2009-12-01), see jtidy.sourceforge.net">
<meta charset="utf-8"><script type="text/javascript" src=
"/some.js">
</script>

    <title></title>
  </head>

  <body>
    <h1>Hello World</h1>

    <p>Here is an interesting paragraph</p>

    <p>And another<small>small</small></p>

    <ul>
      <li>An</li>

      <li>unordered</li>

      <li>list</li>
    </ul>
  </body>
</html>