node

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 4 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attribute

type Attribute struct {
	Key   string
	Value string
}

Attribute represents an HTML attribute with a key-value pair

type ConditionalBuilder

type ConditionalBuilder struct {
	// contains filtered or unexported fields
}

ConditionalBuilder provides a fluent API for conditional rendering. It allows you to specify different content based on a boolean condition.

Nil nodes are safely ignored - if a nil node is provided to True() or False(), it will not be stored and nothing will be rendered for that path.

Usage:

Condition(user.IsLoggedIn).
    True(p.Text("Welcome back!")).
    False(p.Text("Please log in"))

func Condition

func Condition(condition bool) *ConditionalBuilder

Condition creates a new conditional builder with the given boolean condition.

func Unless

func Unless(condition bool, node Node) *ConditionalBuilder

Unless renders the node only when the condition is false. This is a shorthand for Condition(cond).False(node).

Usage:

Unless(user.IsLoggedIn, a.New().Href("/login").Text("Sign in"))

func When

func When(condition bool, node Node) *ConditionalBuilder

When renders the node only when the condition is true. This is a shorthand for Condition(cond).True(node).

Usage:

When(user.IsAdmin, span.Static("Admin"))

func (*ConditionalBuilder) DynamicKey added in v0.2.0

func (c *ConditionalBuilder) DynamicKey() string

DynamicKey returns an empty string — conditionals do not carry tracking keys.

func (*ConditionalBuilder) False

func (c *ConditionalBuilder) False(node Node) *ConditionalBuilder

False sets the node to render when the condition is false. If node is nil (explicit or typed nil pointer), it is not stored.

func (*ConditionalBuilder) IsDynamic added in v0.2.0

func (c *ConditionalBuilder) IsDynamic() bool

IsDynamic returns true — conditionals always contain dynamic content because their output depends on a runtime condition.

func (*ConditionalBuilder) Nodes

func (c *ConditionalBuilder) Nodes() []Node

Nodes returns only the active branch — the one that Render will actually produce output for. Returning both branches would mislead tree walkers into believing that inactive content exists in the rendered tree, which breaks the Differ's structural change detection for keyed elements.

func (*ConditionalBuilder) Render

func (c *ConditionalBuilder) Render(w ...io.Writer) []byte

Render generates the HTML representation based on the condition. If a writer is provided, the output is written to it and nil is returned. If no writer is provided, the output is returned as a byte slice.

func (*ConditionalBuilder) RenderBuilder

func (c *ConditionalBuilder) RenderBuilder(buf *bytes.Buffer)

RenderBuilder writes the HTML representation directly to a buffer. Renders the appropriate node based on the condition.

func (*ConditionalBuilder) True

True sets the node to render when the condition is true. If node is nil (explicit or typed nil pointer), it is not stored.

type Dynamic

type Dynamic interface {
	IsDynamic() bool
	DynamicKey() string
}

Dynamic represents nodes that contain dynamic content requiring re-evaluation on each render. IsDynamic reports whether the node produces different output across renders. DynamicKey returns the developer-assigned key used by the diff engine to track changes across renders. Nodes without a key return an empty string.

type Element

type Element interface {
	Node

	SetAttribute(key string, value string)

	// RenderOpen and RenderClose split the element's rendering so that JIT can
	// cache the opening tag separately from the children.
	// For example: RenderOpen writes <div class="container">, RenderClose writes </div>.
	RenderOpen(buf *bytes.Buffer)
	RenderClose(buf *bytes.Buffer)
}

Element extends Node for HTML elements that have attributes and an open/close tag structure. Not all nodes are elements — text nodes, function components, and conditionals are not. This separation allows extensions (htmx, turbo, shoelace) to accept only types that genuinely support attributes, and allows JIT compilation to pre-render static wrapper tags independently of dynamic content.

type FuncsComponent added in v0.2.0

type FuncsComponent struct {
	// contains filtered or unexported fields
}

FuncsComponent enables dynamic content generation of multiple nodes. The function is called during rendering to generate the actual node content. This is useful for generating lists of items, e.g. from a loop.

Nil nodes are safely ignored - any nil nodes in the returned slice will be skipped.

Usage:

Funcs(func() []Node {
    nodes := []Node{}
    for _, item := range items {
        nodes = append(nodes, li.Text(item.Name))
    }
    return nodes
})

func Funcs added in v0.2.0

func Funcs(fn func() []Node) *FuncsComponent

Funcs creates a new function component that will call the provided function during rendering to generate a slice of nodes. It is the plural form of Func — use Func when the function returns a single Node, and Funcs when it returns a slice.

func (*FuncsComponent) DynamicKey added in v0.2.0

func (f *FuncsComponent) DynamicKey() string

DynamicKey returns an empty string — function components do not carry tracking keys.

func (*FuncsComponent) IsDynamic added in v0.2.0

func (f *FuncsComponent) IsDynamic() bool

IsDynamic returns true — function components always contain dynamic content because their output depends on a function call at render time.

func (*FuncsComponent) Nodes added in v0.2.0

func (f *FuncsComponent) Nodes() []Node

Nodes evaluates the function and returns its output. Returning an empty slice would hide any keyed children from tree walkers, which breaks the Differ's ability to detect patches and structural changes for elements generated by function components.

func (*FuncsComponent) Render added in v0.2.0

func (f *FuncsComponent) Render(w ...io.Writer) []byte

Render generates the HTML representation by calling the function. If a writer is provided, the output is written to it and nil is returned. If no writer is provided, the output is returned as a byte slice.

func (*FuncsComponent) RenderBuilder added in v0.2.0

func (f *FuncsComponent) RenderBuilder(buf *bytes.Buffer)

RenderBuilder writes the HTML representation directly to a buffer. Calls the function to get the actual nodes and renders them. Nil nodes are safely ignored.

type FunctionComponent

type FunctionComponent struct {
	// contains filtered or unexported fields
}

FunctionComponent enables dynamic content generation through function calls. The function is called during rendering to generate the actual node content. This is useful for complex conditional logic, loops, and data transformations.

Nil nodes are safely ignored - if the function returns nil, nothing will be rendered.

Usage:

Func(func() Node {
    if user.IsLoggedIn {
        return div.Text("Welcome back!")
    }
    return div.Text("Please log in")
})

func Func

func Func(fn func() Node) *FunctionComponent

Func creates a new function component that will call the provided function during rendering to generate the actual node content.

func (*FunctionComponent) DynamicKey added in v0.2.0

func (f *FunctionComponent) DynamicKey() string

DynamicKey returns an empty string — function components do not carry tracking keys.

func (*FunctionComponent) IsDynamic added in v0.2.0

func (f *FunctionComponent) IsDynamic() bool

IsDynamic returns true — function components always contain dynamic content because their output depends on a function call at render time.

func (*FunctionComponent) Nodes

func (f *FunctionComponent) Nodes() []Node

Nodes evaluates the function and returns its output. Returning an empty slice would hide any keyed children from tree walkers, which breaks the Differ's ability to detect patches and structural changes for elements generated by function components.

func (*FunctionComponent) Render

func (f *FunctionComponent) Render(w ...io.Writer) []byte

Render generates the HTML representation by calling the function. If a writer is provided, the output is written to it and nil is returned. If no writer is provided, the output is returned as a byte slice.

func (*FunctionComponent) RenderBuilder

func (f *FunctionComponent) RenderBuilder(buf *bytes.Buffer)

RenderBuilder writes the HTML representation directly to a buffer. Calls the function to get the actual node and renders it. Nil nodes are safely ignored.

type Node

type Node interface {
	// Render returns the HTML as a byte slice, or writes it to the provided writer.
	// Use this for top-level rendering where you need the final output.
	Render(w ...io.Writer) []byte

	// RenderBuilder writes HTML into a shared buffer to avoid allocations
	// when composing a tree of nodes. Parent nodes call this on their children.
	RenderBuilder(*bytes.Buffer)

	// Nodes returns the children that will be rendered. For conditionals
	// this is the active branch only; for function components this is the
	// evaluated output. Tree walkers rely on this matching Render output.
	Nodes() []Node
}

Node represents any renderable item in an HTML document tree. This is the base contract that all renderable types satisfy — text, elements, function components, and conditionals alike.

Jump to

Keyboard shortcuts

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