Construct HTML elements in Go. Can be used together with html/template
templates.
elem := dom.Element("div",
dom.Attrs(
"class", "1 2 3",
"data-foo", `4<'"5"'>6`,
),
dom.InnerText("<oops>789</oops>"),
dom.P(
dom.Attrs(),
dom.InnerHTML("<strong>10</strong>"),
),
)
representing HTML
<div class="1 2 3" data-foo="4<'"5"'>6">
<oops>789</oops>
<p><strong>10</strong></p>
</div>
Indented for illustrative purpose; there are no newlines introduced.
Notice the text values added via InnerText
are html safe and InnerHTML
trusts your raw html
fmt.Println(elem.HTML())
or in a http.HandlerFunc
w.Write([]byte(elem.HTML()))
tmpl.ExecuteTemplate(w, "index.html", elem.HTML())
with index.html
content being
<!DOCTYPE html>
<html>
<head><title>dom-go</title></head>
<body>{{ . }}</body>
</html>