htmlg: github.com/shurcooL/htmlg Index | Examples | Files

package htmlg

import "github.com/shurcooL/htmlg"

Package htmlg contains helper funcs for generating HTML nodes and rendering them. Context-aware escaping is done just like in html/template, making it safe against code injection.

Note: This package is quite experimental in nature, so its API is susceptible to more frequent changes than the average package. This is necessary in order to keep this package useful.

Code:

// Context-aware escaping is done just like in html/template.
html := htmlg.Render(
    htmlg.Text("Hi & how are you, "),
    htmlg.A("Gophers", "https://golang.org/"),
    htmlg.Text("? <script> is a cool gopher."),
)

fmt.Fprintln(os.Stdout, html)

Output:

Hi &amp; how are you, <a href="https://golang.org/">Gophers</a>? &lt;script&gt; is a cool gopher.

Index

Examples

Package Files

elements.go htmlg.go

func A Uses

func A(s, href string) *html.Node

A returns an anchor element <a href="{{.href}}">{{.s}}</a>.

func AppendChildren Uses

func AppendChildren(n *html.Node, cs ...*html.Node)

AppendChildren adds nodes cs as children of n.

It will panic if any of cs already has a parent or siblings.

Code:

div := htmlg.Div(
    htmlg.Text("Go "),
)
htmlg.AppendChildren(div, component.Link{
    Text:   "there",
    URL:    "https://golang.org",
    NewTab: true,
}.Render()...)
div.AppendChild(
    htmlg.Text("for more!"),
)

err := html.Render(os.Stdout, div)
if err != nil {
    panic(err)
}

Output:

<div>Go <a href="https://golang.org" target="_blank">there</a>for more!</div>

func DD Uses

func DD(nodes ...*html.Node) *html.Node

DD returns a dd element <dd>{{range .nodes}}{{.}}{{end}}</dd>.

func DL Uses

func DL(nodes ...*html.Node) *html.Node

DL returns a dl element <dl>{{range .nodes}}{{.}}{{end}}</dl>.

func DT Uses

func DT(nodes ...*html.Node) *html.Node

DT returns a dt element <dt>{{range .nodes}}{{.}}{{end}}</dt>.

func Div Uses

func Div(nodes ...*html.Node) *html.Node

Div returns a div element <div>{{range .nodes}}{{.}}{{end}}</div>.

Div is experimental and may be changed or removed.

func DivClass Uses

func DivClass(class string, nodes ...*html.Node) *html.Node

DivClass returns a div element <div class="{{.class}}">{{range .nodes}}{{.}}{{end}}</div>.

DivClass is experimental and may be changed or removed.

func H1 Uses

func H1(nodes ...*html.Node) *html.Node

H1 returns a h1 element <h1>{{range .nodes}}{{.}}{{end}}</h1>.

func H2 Uses

func H2(nodes ...*html.Node) *html.Node

H2 returns a h2 element <h2>{{range .nodes}}{{.}}{{end}}</h2>.

func H3 Uses

func H3(nodes ...*html.Node) *html.Node

H3 returns a h3 element <h3>{{range .nodes}}{{.}}{{end}}</h3>.

func H4 Uses

func H4(nodes ...*html.Node) *html.Node

H4 returns a h4 element <h4>{{range .nodes}}{{.}}{{end}}</h4>.

func LI Uses

func LI(nodes ...*html.Node) *html.Node

LI returns a li element <li>{{range .nodes}}{{.}}{{end}}</li>.

func LIClass Uses

func LIClass(class string, nodes ...*html.Node) *html.Node

LIClass returns a div element <li class="{{.class}}">{{range .nodes}}{{.}}{{end}}</li>.

LIClass is experimental and may be changed or removed.

func P Uses

func P(nodes ...*html.Node) *html.Node

P returns a p element <p>{{range .nodes}}{{.}}{{end}}</p>.

func Render Uses

func Render(nodes ...*html.Node) string

Render renders HTML nodes, returning result as a string. Context-aware escaping is done just like in html/template when rendering nodes.

func RenderComponents Uses

func RenderComponents(w io.Writer, components ...Component) error

RenderComponents renders components into HTML, writing result to w. Context-aware escaping is done just like in html/template when rendering nodes.

func RenderComponentsString Uses

func RenderComponentsString(components ...Component) string

RenderComponentsString renders components into HTML, returning result as a string. Context-aware escaping is done just like in html/template when rendering nodes.

func Span Uses

func Span(nodes ...*html.Node) *html.Node

Span returns a span element <span>{{range .nodes}}{{.}}{{end}}</span>.

Span is experimental and may be changed or removed.

func SpanClass Uses

func SpanClass(class string, nodes ...*html.Node) *html.Node

SpanClass returns a span element <span class="{{.class}}">{{range .nodes}}{{.}}{{end}}</span>.

SpanClass is experimental and may be changed or removed.

func Strong Uses

func Strong(s string) *html.Node

Strong returns a strong text node.

func TD Uses

func TD(nodes ...*html.Node) *html.Node

TD returns a td element <td>{{range .nodes}}{{.}}{{end}}</td>.

func TR Uses

func TR(nodes ...*html.Node) *html.Node

TR returns a tr element <tr>{{range .nodes}}{{.}}{{end}}</tr>.

func Text Uses

func Text(s string) *html.Node

Text returns a plain text node.

func UL Uses

func UL(nodes ...*html.Node) *html.Node

UL returns a ul element <ul>{{range .nodes}}{{.}}{{end}}</ul>.

func ULClass Uses

func ULClass(class string, nodes ...*html.Node) *html.Node

ULClass returns a div element <ul class="{{.class}}">{{range .nodes}}{{.}}{{end}}</ul>.

ULClass is experimental and may be changed or removed.

type Component Uses

type Component interface {
    Render() []*html.Node
}

Component is anything that can render itself into HTML nodes.

type NodeComponent Uses

type NodeComponent html.Node

NodeComponent is a wrapper that makes a Component from a single html.Node.

Code:

heading := htmlg.NodeComponent{
    Type: html.ElementNode, Data: atom.H2.String(),
    FirstChild: htmlg.Text("This heading is an htmlg.Component"),
}

err := htmlg.RenderComponents(os.Stdout, heading)
if err != nil {
    panic(err)
}

Output:

<h2>This heading is an htmlg.Component</h2>

func (NodeComponent) Render Uses

func (n NodeComponent) Render() []*html.Node

Package htmlg imports 5 packages (graph) and is imported by 35 packages. Updated 2017-09-18. Refresh now. Tools for package owners.