Documentation ¶
Overview ¶
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.
Example ¶
package main import ( "fmt" "os" "github.com/shurcooL/htmlg" ) func main() { // 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 & how are you, <a href="https://golang.org/">Gophers</a>? <script> is a cool gopher.
Index ¶
- func A(s, href string) *html.Node
- func AppendChildren(n *html.Node, cs ...*html.Node)
- func Code(nodes ...*html.Node) *html.Node
- func DD(nodes ...*html.Node) *html.Node
- func DL(nodes ...*html.Node) *html.Node
- func DT(nodes ...*html.Node) *html.Node
- func Div(nodes ...*html.Node) *html.Node
- func DivClass(class string, nodes ...*html.Node) *html.Node
- func H1(nodes ...*html.Node) *html.Node
- func H2(nodes ...*html.Node) *html.Node
- func H3(nodes ...*html.Node) *html.Node
- func H4(nodes ...*html.Node) *html.Node
- func LI(nodes ...*html.Node) *html.Node
- func LIClass(class string, nodes ...*html.Node) *html.Node
- func P(nodes ...*html.Node) *html.Node
- func Pre(nodes ...*html.Node) *html.Node
- func Render(nodes ...*html.Node) string
- func RenderComponents(w io.Writer, components ...Component) error
- func RenderComponentsString(components ...Component) string
- func Span(nodes ...*html.Node) *html.Node
- func SpanClass(class string, nodes ...*html.Node) *html.Node
- func Strong(s string) *html.Node
- func TD(nodes ...*html.Node) *html.Node
- func TR(nodes ...*html.Node) *html.Node
- func Text(s string) *html.Node
- func UL(nodes ...*html.Node) *html.Node
- func ULClass(class string, nodes ...*html.Node) *html.Node
- type Component
- type NodeComponent
- type Nodes
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendChildren ¶
AppendChildren adds nodes cs as children of n.
It will panic if any of cs already has a parent or siblings.
Example ¶
package main import ( "os" "github.com/shurcooL/component" "github.com/shurcooL/htmlg" "golang.org/x/net/html" ) func main() { 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 Div ¶
Div returns a div element <div>{{range .nodes}}{{.}}{{end}}</div>.
Div is experimental and may be changed or removed.
func DivClass ¶
DivClass returns a div element <div class="{{.class}}">{{range .nodes}}{{.}}{{end}}</div>.
DivClass is experimental and may be changed or removed.
func LIClass ¶
LIClass returns a div element <li class="{{.class}}">{{range .nodes}}{{.}}{{end}}</li>.
LIClass is experimental and may be changed or removed.
func Render ¶
Render renders HTML nodes, returning result as a string. Context-aware escaping is done just like in html/template when rendering nodes.
func RenderComponents ¶
RenderComponents renders components into HTML, writing result to w. Context-aware escaping is done just like in html/template when rendering nodes.
func RenderComponentsString ¶
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 ¶
Span returns a span element <span>{{range .nodes}}{{.}}{{end}}</span>.
Span is experimental and may be changed or removed.
func SpanClass ¶
SpanClass returns a span element <span class="{{.class}}">{{range .nodes}}{{.}}{{end}}</span>.
SpanClass is experimental and may be changed or removed.
Types ¶
type NodeComponent ¶
NodeComponent is a wrapper that makes a Component from a single html.Node.
Example ¶
package main import ( "os" "github.com/shurcooL/htmlg" "golang.org/x/net/html" "golang.org/x/net/html/atom" ) func main() { 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 ¶
func (n NodeComponent) Render() []*html.Node
type Nodes ¶
Nodes implements the Component interface from a slice of HTML nodes.
The Render method always returns the same references to existing nodes, and as a result, it is unsuitable to be rendered and attached to other HTML nodes more than once. It is suitable to rendered directly into HTML multiple times, or attached to an existing node once.
Nodes is experimental and may be changed or removed.
Example ¶
package main import ( "os" "github.com/shurcooL/htmlg" ) func main() { err := htmlg.RenderComponents(os.Stdout, htmlg.Nodes{ htmlg.Text("Hi & how are you, "), htmlg.A("Gophers", "https://golang.org/"), htmlg.Text("? <script> is a cool gopher."), }) if err != nil { panic(err) } }
Output: Hi & how are you, <a href="https://golang.org/">Gophers</a>? <script> is a cool gopher.