choonkeat / dom-go

Construct HTML elements in Go. Can be used together with `html/template` templates.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DOM

Construct HTML elements in Go. Can be used together with html/template templates.

Example

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&lt;&#39;&#34;5&#34;&#39;&gt;6">
    &lt;oops&gt;789&lt;/oops&gt;
    <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

Usage (Standalone)

fmt.Println(elem.HTML())

or in a http.HandlerFunc

w.Write([]byte(elem.HTML()))

Usage (html/template)

tmpl.ExecuteTemplate(w, "index.html", elem.HTML())

with index.html content being

<!DOCTYPE html>
<html>
    <head><title>dom-go</title></head>
    <body>{{ . }}</body>
</html>

About

Construct HTML elements in Go. Can be used together with `html/template` templates.

License:MIT License


Languages

Language:Go 99.7%Language:Makefile 0.3%