elem

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package elem provides types and utils for node elements.

The core abstractions are:

  • Node: A lazy-evaluated element.
  • Obj: A static representation of evaluated content.

Element utils include:

  • New: Produces an element, returns a Scope to complete with child elements (e.g. <div>scope goes here</div>).
  • Void: A self-closing element (e.g., <br/>, <img/>).
  • Raw: Raw document content (text, HTML).
  • Scope: A function to call to fill in child elements.
  • Bundle: A slice of multiple elements.
  • Seq: A sequence of multiple elements.
  • Cons: A head element followed by a tail of elements.
  • Noop: An empty element that produces no output.
  • If: A conditional element.
  • IfElse: A choice between elements.
  • Fn: A dynamically produced element.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bundle

type Bundle []Node

Bundle is a slice of elements. These elements will be rendered adjacent to each other.

func (Bundle) Eval

func (b Bundle) Eval(ctx context.Context) (Obj, error)

type Fn

type Fn func(ctx context.Context) (Node, error)

Fn is a type of node that generates the output dynamically.

func (Fn) Eval

func (fn Fn) Eval(ctx context.Context) (Obj, error)

type Node

type Node interface {
	Eval(ctx context.Context) (Obj, error)
}

func Comment

func Comment(content string) Node

Comment creates an HTML comment with string-escaped text content.

func Cons

func Cons(head Node, tail ...Node) Node

Cons is a functional convenience for producing a sequence of elements: starting with the head element, followed by the tail elements. These elements will be rendered adjacent to each other.

func If

func If(x bool, v Node) Node

If returns v if x is true, and returns a Noop otherwise.

func IfElse

func IfElse(x bool, trueCase Node, falseCase Node) Node

IfElse returns trueCase if x is true, and returns falseCase otherwise.

func Noop

func Noop() Node

Noop produces an element that is not rendered.

func Raw

func Raw(v string) Node

Raw creates an element that renders the given string directly (no escaping).

func Void

func Void(name string, attrs ...attr.Node) Node

Void creates a self-closing void element (e.g. <br/>, <input/>).

type Obj

type Obj struct {
	// Tag is the element tag name. Empty for raw content or bundles.
	Tag string
	// Raw is pre-rendered HTML content (used for raw/comment nodes).
	// When set, Tag is a sentinel (e.g. "RAW", "COMMENT") to avoid squashing.
	Raw string
	// Void indicates a self-closing element (e.g. <br/>, <input/>).
	Void bool
	// Attribs holds the element's attributes.
	Attribs attr.Seq
	// Children holds the element's child elements.
	Children iter.Seq[Node]
}

Obj is the evaluated representation of an element.

func (Obj) Eval

func (o Obj) Eval(ctx context.Context) (Obj, error)

type Scope

type Scope func(children ...Node) Node

Scope is returned by non-void element constructors. Call it with child elements to produce an elem.Node. It also implements elem.Node directly (evaluates with no children).

func New

func New(name string, attrs ...attr.Node) Scope

New creates a non-void element constructor (e.g. <div>scope content</div>). Returns a Scope: call it with child elements to produce an Elem.

func (Scope) Eval

func (s Scope) Eval(ctx context.Context) (Obj, error)

Eval implements Elem for Scope: an unopened Scope itself counts as an element without content.

type Seq

type Seq iter.Seq[Node]

Seq is a sequence of elements. These elements will be rendered adjacent to each other.

func (Seq) Eval

func (s Seq) Eval(ctx context.Context) (Obj, error)

Jump to

Keyboard shortcuts

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