runtime

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IncludeZeroTruthy

func IncludeZeroTruthy(v any) (bool, error)

IncludeZeroTruthy reports whether v should be treated as truthy when the includeZero option is set: it returns true if v is numeric zero or if IsTruthy(v) is true. Used by {{#if}} / {{#unless}} with includeZero=true.

func IsNumericZero

func IsNumericZero(v any) (bool, error)

IsNumericZero reports whether v is a numeric type with value zero. It handles json.Number (from decoding JSON with UseNumber()), Go numeric types, and numeric kinds via reflection when ReflectUsageLevel allows.

func IsTruthy

func IsTruthy(value any) (bool, error)

IsTruthy reports whether a value should be treated as true in block helpers.

func LookupPath

func LookupPath(root any, path string) any

LookupPath returns the value at the dot-separated path from root (e.g. "title", "user.name"). Root can be map[string]any or implement Raw() any returning a map. Used for @root.xxx in partials.

func MergePartialContext

func MergePartialContext(base, additions map[string]any) map[string]any

MergePartialContext returns a new map with base keys/values plus additions (additions override). Used for partial context: current/explicit context merged with hash (e.g. note="thanks"). If base is nil, returns a copy of additions.

func MissingPartial

func MissingPartial(name string) error

MissingPartial formats a missing partial error.

func MissingPartialOutput

func MissingPartialOutput(w io.Writer, name string)

MissingPartialOutput is used for dynamic partials only: when the partial name is not found, it writes the error message (HTML comment) and logs with log.Error; the render continues without failing. If w implements LazyBlockWriter, the output is registered as a lazy block and written on Flush.

func SetReflectUsageLevel added in v0.1.2

func SetReflectUsageLevel(level ReflectUsageLevel)

SetReflectUsageLevel sets the reflect usage level (USE, WARN, ERROR). Default is WARN.

func Stringify

func Stringify(v any) string

Stringify converts a value to its string representation.

func WriteEscaped

func WriteEscaped(w io.Writer, v any) error

WriteEscaped writes an escaped value into the writer.

func WriteRaw

func WriteRaw(w io.Writer, v any) error

WriteRaw writes a raw value into the writer.

Types

type Blocks

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

Blocks holds content for layout block/partial: child templates register content with {{#partial "name"}}...{{/partial}}, layout outputs it with {{#block "name"}}default{{/block}}. When Blocks is nil, {{#block}} renders only its default body and {{#partial}} renders its body to the writer.

func NewBlocks

func NewBlocks() *Blocks

NewBlocks returns a new Blocks store for use with layout templates.

func (*Blocks) Get

func (b *Blocks) Get(name string) (string, bool)

Get returns the content registered for name and true, or "" and false.

func (*Blocks) Set

func (b *Blocks) Set(name, content string)

Set stores content for the given block name (used by {{#partial}}).

type Hash

type Hash map[string]any

Hash represents named helper arguments.

func HashArg

func HashArg(args []any) (Hash, bool)

HashArg returns the trailing Hash argument if present. Deprecated: helpers now receive runtime.HelperArgs; use args.HashArgs or args.GetHash / GetHashString / GetHashNumber instead.

type Helper

type Helper func(args HelperArgs) (any, error)

Helper is a user-defined function invoked from a template. It receives HelperArgs; values are resolved by the compiler. For block helpers, call args.BlockFn() or args.InverseFn() with no arguments; the writer is already captured in the closure.

type HelperArgs added in v0.1.3

type HelperArgs struct {
	HashArgs  map[string]any // named (hash) arguments
	Args      []any          // positional arguments
	BlockFn   func() error   // when IsBlock: closure that renders the main block (writer captured by compiler); nil otherwise
	InverseFn func() error   // when IsBlock and else exists: closure that renders the else block; may be nil
	IsBlock   bool           // true for block helper invocations
}

HelperArgs is the argument bundle passed to every helper. Values are resolved by the compiler; the helper receives typed args and hash.

func (HelperArgs) GetArg added in v0.1.3

func (a HelperArgs) GetArg(idx int) any

GetArg returns the positional argument at index, or nil if out of bounds.

func (HelperArgs) GetHash added in v0.1.3

func (a HelperArgs) GetHash(key string) any

GetHash returns the hash argument for key, or nil if missing or HashArgs is nil.

func (HelperArgs) GetHashNumber added in v0.1.3

func (a HelperArgs) GetHashNumber(key string) (float64, error)

GetHashNumber converts the hash value for key to float64. Returns (0, nil) if missing or nil.

func (HelperArgs) GetHashString added in v0.1.3

func (a HelperArgs) GetHashString(key string) string

GetHashString returns the stringified hash value for key, or empty string.

func (HelperArgs) GetNumber added in v0.1.3

func (a HelperArgs) GetNumber(idx int) (float64, error)

GetNumber converts the argument at index to float64. Returns (0, nil) for nil or out of bounds.

func (HelperArgs) GetString added in v0.1.3

func (a HelperArgs) GetString(idx int) string

GetString returns the stringified argument at index, or empty string.

type LazyBlockWriter

type LazyBlockWriter interface {
	io.Writer
	// WriteLazyBlock registers a block to be written at Flush. The block receives
	// the underlying writer and may write the "partial \"\" is not defined" message etc.
	WriteLazyBlock(block func(io.Writer))
	// Flush writes any buffered data and evaluates lazy blocks in order.
	Flush() error
}

LazyBlockWriter is an io.Writer that can register lazy output blocks. A lazy block is written when Flush is called, preserving order with prior Write calls.

type LazyWriter

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

LazyWriter wraps w and implements LazyBlockWriter: Write and WriteLazyBlock are queued in order; Flush drains the queue to w.

func NewLazyWriter

func NewLazyWriter(w io.Writer) *LazyWriter

NewLazyWriter returns a LazyBlockWriter that flushes to w.

func (*LazyWriter) Flush

func (l *LazyWriter) Flush() error

Flush writes all queued direct bytes and runs lazy blocks in order.

func (*LazyWriter) Write

func (l *LazyWriter) Write(p []byte) (n int, err error)

Write implements io.Writer by queuing the bytes; they are written on Flush.

func (*LazyWriter) WriteLazyBlock

func (l *LazyWriter) WriteLazyBlock(block func(io.Writer))

WriteLazyBlock registers a block to run at Flush (writes to l.w).

type ReflectUsageLevel added in v0.1.2

type ReflectUsageLevel int

ReflectUsageLevel is the level for reflect fallback branches.

const (
	ReflectUse ReflectUsageLevel = iota
	ReflectWarn
	ReflectError
)

ReflectUsageLevel controls how reflection is used when type switches don't cover a value.

func GetReflectUsageLevel added in v0.1.2

func GetReflectUsageLevel() ReflectUsageLevel

GetReflectUsageLevel returns the current reflect usage level.

type SafeString

type SafeString string

SafeString marks a value as pre-escaped HTML.

Jump to

Keyboard shortcuts

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