node

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 4 Imported by: 6

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) 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) Nodes

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

Nodes returns the potential child nodes of the ConditionalBuilder.

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 {
	// Dynamic returns true if this node contains dynamic content that must be re-evaluated on each render.
	Dynamic() bool
}

Dynamic represents nodes that contain dynamic content requiring re-evaluation on each render. This interface is used by the JIT compiler to identify nodes that cannot be pre-rendered.

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 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) Nodes

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

Nodes returns an empty slice as FunctionComponent nodes do not have static children.

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 FunctionsComponent

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

FunctionsComponent 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:

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

func FuncNodes

func FuncNodes(fn func() []Node) *FunctionsComponent

FuncNodes creates a new function component that will call the provided function during rendering to generate a slice of nodes.

func (*FunctionsComponent) Nodes

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

Nodes returns an empty slice as FunctionsComponent nodes do not have static children.

func (*FunctionsComponent) Render

func (f *FunctionsComponent) 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 (*FunctionsComponent) RenderBuilder

func (f *FunctionsComponent) 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 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 direct children of this node.
	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