Documentation ¶
Overview ¶
Package gomponents provides view components in Go, that render to HTML 5. The primary interface is a Node, which has a single function Render, which should render the Node to a string. Furthermore, NodeFunc is a function which implements the Node interface by calling itself on Render. All DOM elements and attributes can be created by using the El and Attr functions. The package also provides a lot of convenience functions for creating elements and attributes with the most commonly used parameters. If they don't suffice, a fallback to El and Attr is always possible.
Index ¶
- Constants
- type Node
- func Attr(name string, value ...string) Node
- func El(name string, children ...Node) Node
- func Group(children []Node) Node
- func If(condition bool, n Node) Node
- func Map(length int, cb func(i int) Node) []Node
- func Raw(t string) Node
- func Text(t string) Node
- func Textf(format string, a ...interface{}) Node
- type NodeFunc
- type NodeType
Examples ¶
Constants ¶
const ( ElementType = NodeType(iota) AttributeType )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Node ¶
Node is a DOM node that can Render itself to a io.Writer.
func Attr ¶
Attr creates an attribute DOM Node with a name and optional value. If only a name is passed, it's a name-only (boolean) attribute (like "required"). If a name and value are passed, it's a name-value attribute (like `class="header"`). More than one value make Attr panic. Use this if no convenience creator exists.
Example (Bool) ¶
package main import ( "os" g "github.com/maragudk/gomponents" ) func main() { e := g.El("input", g.Attr("required")) _ = e.Render(os.Stdout) }
Output: <input required>
Example (Name_value) ¶
package main import ( "os" g "github.com/maragudk/gomponents" ) func main() { e := g.El("div", g.Attr("id", "hat")) _ = e.Render(os.Stdout) }
Output: <div id="hat"></div>
func El ¶
El creates an element DOM Node with a name and child Nodes. See https://dev.w3.org/html5/spec-LC/syntax.html#elements-0 for how elements are rendered. No tags are ever omitted from normal tags, even though it's allowed for elements given at https://dev.w3.org/html5/spec-LC/syntax.html#optional-tags If an element is a void kind, non-attribute children nodes are ignored. Use this if no convenience creator exists.
Example ¶
package main import ( "os" g "github.com/maragudk/gomponents" ) func main() { e := g.El("div", g.El("span")) _ = e.Render(os.Stdout) }
Output: <div><span></span></div>
func Group ¶ added in v0.7.0
Group multiple Nodes into one Node. Useful for concatenation of Nodes in variadic functions. The resulting Node cannot Render directly, trying it will panic. Render must happen through a parent element created with El or a helper.
func If ¶ added in v0.13.0
If condition is true, return the given Node. Otherwise, return nil. This helper function is good for inlining elements conditionally.
Example ¶
package main import ( "os" g "github.com/maragudk/gomponents" ) func main() { showMessage := true e := g.El("div", g.If(showMessage, g.El("span", g.Text("You lost your hat!"))), g.If(!showMessage, g.El("span", g.Text("No messages."))), ) _ = e.Render(os.Stdout) }
Output: <div><span>You lost your hat!</span></div>
func Map ¶ added in v0.11.0
Map something enumerable to a list of Nodes.
Example ¶
package main import ( "os" g "github.com/maragudk/gomponents" ) func main() { items := []string{"party hat", "super hat"} e := g.El("ul", g.Group(g.Map(len(items), func(i int) g.Node { return g.El("li", g.Text(items[i])) }))) _ = e.Render(os.Stdout) }
Output: <ul><li>party hat</li><li>super hat</li></ul>
func Raw ¶
Raw creates a text DOM Node that just Renders the unescaped string t.
Example ¶
package main import ( "os" g "github.com/maragudk/gomponents" ) func main() { e := g.El("span", g.Raw(`<button onclick="javascript:alert('Party time!')">Party hats</button> > normal hats.`), ) _ = e.Render(os.Stdout) }
Output: <span><button onclick="javascript:alert('Party time!')">Party hats</button> > normal hats.</span>
func Text ¶
Text creates a text DOM Node that Renders the escaped string t.
Example ¶
package main import ( "os" g "github.com/maragudk/gomponents" ) func main() { e := g.El("span", g.Text("Party hats > normal hats.")) _ = e.Render(os.Stdout) }
Output: <span>Party hats > normal hats.</span>
func Textf ¶ added in v0.4.0
Textf creates a text DOM Node that Renders the interpolated and escaped string t.
Example ¶
package main import ( "os" g "github.com/maragudk/gomponents" ) func main() { e := g.El("span", g.Textf("%v party hats > %v normal hats.", 2, 3)) _ = e.Render(os.Stdout) }
Output: <span>2 party hats > 3 normal hats.</span>
Directories ¶
Path | Synopsis |
---|---|
Package assert provides testing helpers.
|
Package assert provides testing helpers. |
Package components provides high-level components and helpers that are composed of low-level elements and attributes.
|
Package components provides high-level components and helpers that are composed of low-level elements and attributes. |
examples
|
|
Package html provides common HTML elements and attributes.
|
Package html provides common HTML elements and attributes. |
Package svg provides common SVG elements and attributes.
|
Package svg provides common SVG elements and attributes. |