by @maragudk
Guide for working with gomponents, a pure Go HTML component library. Use this skill when reading or writing gomponents code, or when building HTML views in Go applications.
gomponents is a pure Go HTML component library that treats HTML elements as composable Go values. Everything is built on the Node interface, making HTML construction type-safe and composable.
Use this skill when:
Everything in gomponents implements the Node interface:
type Node interface {
Render(w io.Writer) error
}
El(name string, children ...Node) - Create custom HTML elementsAttr(name string, value ...string) - Create custom attributesMost standard HTML5 elements and attributes are available as functions in the html package:
Div(), Span(), P(), A(), etc.Class(), ID(), Href(), Src(), etc.Note: nil Nodes are ignored during rendering, so it's safe to pass nil nodes to elements.
Text(string) - HTML-escaped text contentTextf(format string, args...) - Formatted, escaped textRaw(string) - Unescaped HTMLRawf(format string, args...) - Formatted, unescaped contentGroup([]Node) - Combine multiple nodesMap[T]([]T, func(T) Node) - Transform slices into node sequencesIf(condition bool, node Node) - Conditional renderingIff(condition bool, func() Node) - Lazy conditional rendering (deferred evaluation)Contrary to common Go idioms, dot imports are recommended for gomponents to achieve DSL-like syntax:
import (
. "maragu.dev/gomponents"
. "maragu.dev/gomponents/html"
. "maragu.dev/gomponents/components"
)
This allows writing clean, HTML-like code:
Div(Class("container"),
H1(Text("Hello World")),
P(Text("Welcome to gomponents")),
)
maragu.dev/gomponents - Core i...