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.

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 experimental.go htmlg.go

func A Uses

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

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

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) template.HTML

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

TODO: Return string instead of template.HTML; returning template.HTML has proven to be unhelpful (since so many consumers expect a simple string).

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 RenderComponentsContext Uses

func RenderComponentsContext(ctx context.Context, w io.Writer, components ...ComponentContext) error

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

Deprecated: RenderComponentsContext is deprecated and will be removed soon. It was an experiment, and it turned out not to be a good idea. It makes more sense and scales better to perform service requests in advance (potentially in parallel) and pass static values to components to render.

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 ComponentContext Uses

type ComponentContext interface {
    RenderContext(ctx context.Context) []*html.Node
}

ComponentContext is anything that can render itself into HTML nodes.

Deprecated: ComponentContext is deprecated and will be removed soon. It was an experiment, and it turned out not to be a good idea. It makes more sense and scales better to perform service requests in advance (potentially in parallel) and pass static values to components to render.

Package htmlg imports 7 packages (graph) and is imported by 25 packages. Updated 2017-02-06. Refresh now. Tools for package owners.