gomponents

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2023 License: MIT Imports: 4 Imported by: 0

README

Just testing things out, forked from https://github.com/majkjkragudk/gomponents

Documentation

Overview

Package gomponents provides view components in Go, that render to HTML 5.

The primary interface is a Node. It describes a function Render, which should render the Node to the given writer as a string.

All DOM elements and attributes can be created by using the El and Attr functions. The functions Text, Textf, Raw, and Rawf can be used to create text nodes, either HTML-escaped or unescaped. See also helper functions Group, Map, and If for mapping data to Nodes and inserting them conditionally.

For basic HTML elements and attributes, see the package html. For higher-level HTML components, see the package components. For SVG elements and attributes, see the package svg. For HTTP helpers, see the package http.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StringToBytes

func StringToBytes(str string) []byte

Types

type Node

type Node interface {
	Render(w io.Writer) error
}

Node is a DOM node that can Render itself to a io.Writer.

func Attr

func Attr(name string, value ...string) Node

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)
e := g.El("input", g.Attr("required"))
_ = e.Render(os.Stdout)
Output:

<input required>
Example (Name_value)
e := g.El("div", g.Attr("id", "hat"))
_ = e.Render(os.Stdout)
Output:

<div id="hat"></div>

func El

func El(name string, children ...Node) Node

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 element, non-attribute children nodes are ignored. Use this if no convenience creator exists.

Example
e := g.El("div", g.El("span"))
_ = e.Render(os.Stdout)
Output:

<div><span></span></div>

func Fragment

func Fragment(children ...Node) Node

Fragment groups multiple nodes into one Node. Kind of like React.Fragment. It has an easier api than Group for rendering a collection of nodes without specifiying a parent element.

func Group

func Group(children []Node) Node

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

func If(condition bool, thenNode, elseNode Node) Node

If condition is true, return the given Node. Otherwise, return nil. This helper function is good for inlining elements conditionally.

Example
showMessage := true
e := g.El("div",
	g.If(showMessage, g.El("span", Text("You lost your hat!")), nil),
	g.If(!showMessage, g.El("span", Text("No messages.")), nil),
)
_ = e.Render(os.Stdout)
Output:

<div><span>You lost your hat!</span></div>

func Static

func Static(children ...Node) Node

type NodeFunc

type NodeFunc func(io.Writer) error

NodeFunc is a render function that is also a Node of ElementType.

func (NodeFunc) Render

func (n NodeFunc) Render(w io.Writer) error

Render satisfies Node.

func (NodeFunc) String

func (n NodeFunc) String() string

String satisfies fmt.Stringer.

func (NodeFunc) Type

func (n NodeFunc) Type() NodeType

type NodeType

type NodeType uint8

NodeType describes what type of Node it is, currently either an ElementType or an AttributeType. This decides where a Node should be rendered. Nodes default to being ElementType.

const (
	ElementType NodeType = iota
	AttributeType
)

type TypedNode

type TypedNode interface {
	Node
	Type() NodeType
}

Directories

Path Synopsis
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 http provides adapters to render gomponents in http handlers.
Package http provides adapters to render gomponents in http handlers.
Package hx provides utilities to work with htmx
Package hx provides utilities to work with htmx
internal
assert
Package assert provides testing helpers.
Package assert provides testing helpers.
Package svg provides common SVG elements and attributes.
Package svg provides common SVG elements and attributes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL