node

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 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) SetAttribute

func (c *ConditionalBuilder) SetAttribute(_ string, _ string)

SetAttribute is a no-op for ConditionalBuilder as it does not have attributes.

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

	// RenderOpen writes the opening tag and attributes to the buffer.
	// For example, for a div with class="container", this writes: <div class="container">
	RenderOpen(buf *bytes.Buffer)

	// RenderClose writes the closing tag to the buffer.
	// For example, for a div, this writes: </div>
	// For self-closing elements, this is a no-op.
	RenderClose(buf *bytes.Buffer)
}

Element extends Node with methods for rendering opening and closing tags separately. This interface enables JIT compilation to pre-render static wrapper tags while preserving dynamic content rendering.

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.

func (*FunctionComponent) SetAttribute

func (f *FunctionComponent) SetAttribute(_ string, _ string)

SetAttribute is a no-op for FunctionComponent as it does not have attributes.

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.

func (*FunctionsComponent) SetAttribute

func (f *FunctionsComponent) SetAttribute(_ string, _ string)

SetAttribute is a no-op for FunctionsComponent as it does not have attributes.

type Node

type Node interface {
	// Render generates the complete HTML representation of the node.
	// 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.
	Render(w ...io.Writer) []byte

	// RenderBuilder writes the HTML representation directly to a buffer.
	// This method is used for building complex node trees efficiently
	// by allowing nodes to render into a shared buffer.
	RenderBuilder(*bytes.Buffer)

	// Nodes returns a slice of child nodes.
	// For nodes that do not have children, it returns an empty slice.
	Nodes() []Node

	// SetAttribute sets an attribute on the node.
	// This is the primary method for extensions to add attributes safely.
	SetAttribute(key string, value string)
}

Node represents any element in an HTML document tree. All nodes implement both direct output rendering and efficient buffer rendering for optimal performance in different scenarios.

Jump to

Keyboard shortcuts

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