core

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: 6 Imported by: 0

Documentation

Overview

Package core provides core rendering functionality. Use Render to evaluate a node graph and write it to an io.Writer, or Dump to write to stdout for debugging.

Example (Fallback)

Fallback() catches render errors and substitutes alternative content. Useful for graceful degradation of page sections.

package main

import (
	"context"
	"errors"

	"github.com/protolambda/chord/core"
	"github.com/protolambda/chord/core/attr"
	"github.com/protolambda/chord/core/elem"
	"github.com/protolambda/chord/html/text"
)

func main() {
	unreliable := elem.Fn(func(ctx context.Context) (elem.Node, error) {
		return nil, errors.New("database unavailable")
	})

	core.Dump(core.Fallback(
		elem.New("div")(unreliable),
		func(ctx context.Context, err error) elem.Node {
			return elem.New("div", attr.KV("class", "error"))(
				text.Text("Failed to load content"))
		},
	))
}
Output:
<div class="error">Failed to load content</div>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dump

func Dump(v elem.Node, opts ...Option)

Dump prints the element to stdout, for convenience in testing and debugging.

func DumpCtx

func DumpCtx(ctx context.Context, v elem.Node, opts ...Option)

DumpCtx prints the element (evaluated with ctx) to stdout, for convenience in testing and debugging.

func Fallback

func Fallback(node elem.Node, onErr FallbackFn) elem.Node

Fallback renders an element node, and can fall back the rendering to the given function, in case of an error during rendering.

func Render

func Render(ctx context.Context, v elem.Node, out *strings.Builder, opts ...Option) error

Render renders an element tree to the given string builder.

Types

type FallbackFn

type FallbackFn func(ctx context.Context, err error) elem.Node

FallbackFn is called when a render error occurs to provide alternative content.

type Option

type Option func(*renderConfig)

Option applies a configuration to the renderer.

func WithIndent

func WithIndent() Option

WithIndent enables indentation of elements with two spaces per depth level.

Example
Dump(
	elem.New("div", attr.KV("class", "outer"), attr.KV("id", "root"))(
		elem.New("div", attr.KV("class", "middle"))(
			elem.New("div", attr.KV("class", "inner"))(
				elem.Raw("Hello"),
			),
			elem.New("span")(elem.Raw("World")),
		),
	),
	WithIndent(),
)
Output:
<div class="outer" id="root">
  <div class="middle">
    <div class="inner">
      Hello
    </div>
    <span>
      World
    </span>
  </div>
</div>

Directories

Path Synopsis
Package attr provides types and utils for node attributes.
Package attr provides types and utils for node attributes.
Package elem provides types and utils for node elements.
Package elem provides types and utils for node elements.

Jump to

Keyboard shortcuts

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