template

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompAsButton

func CompAsButton(c *widget.Component) *widget.Button

CompAsButton is the exported equivalent of compAsButton, for use by the root package tests which cannot call the unexported version.

func CompAsLabel

func CompAsLabel(c *widget.Component) *widget.Label

CompAsLabel is the exported equivalent of compAsLabel, for use by the root package tests which cannot call the unexported version.

func EncodeIR

func EncodeIR(node *IRNode) ([]byte, error)

EncodeIR serializes an IRNode tree to the .xmlui binary format.

func EvalExpression

func EvalExpression(node ExprNode, ctx *EvalContext) (any, error)

EvalExpression evaluates an expression AST node in the given context.

func ParseColor

func ParseColor(s string) sg.Color

ParseColor is the exported equivalent of parseColor, for use by the root package tests which cannot call the unexported version.

func ToBool

func ToBool(v any) bool

ToBool is the exported equivalent of toBool, for use by the root package tests.

Types

type AttrSetter

type AttrSetter func(comp *widget.Component, value any)

AttrSetter applies a named attribute value to a custom widget component.

type BinOp

type BinOp int

BinOp identifies a binary operator.

const (
	BinAdd BinOp = iota
	BinSub
	BinMul
	BinDiv
	BinMod
	BinEq
	BinNeq
	BinLt
	BinLte
	BinGt
	BinGte
	BinAnd
	BinOr
)

type DataProvider

type DataProvider interface {
	LookupRef(path string) any
	CallMethod(name string) bool
}

DataProvider is implemented by controllers that support XML template data binding.

type DirectiveType

type DirectiveType int

DirectiveType identifies a structural directive in a compiled template.

const (
	DirectiveIf     DirectiveType = iota // ui:if="expr"
	DirectiveElseIf                      // ui:else-if="expr"
	DirectiveElse                        // ui:else
	DirectiveFor                         // ui:for="item in list"
	DirectiveKey                         // ui:key="expr"
	DirectiveShow                        // ui:show="expr"
)

type EvalContext

type EvalContext struct {
	Provider DataProvider
	Locals   map[string]any
	Parent   *EvalContext
}

EvalContext provides the evaluation environment for expressions.

type ExprBinary

type ExprBinary struct {
	Op    BinOp
	Left  ExprNode
	Right ExprNode
}

ExprBinary is a binary operation (e.g. a + b, x == y).

type ExprConcat

type ExprConcat struct {
	Parts []ExprNode
}

ExprConcat is a string concatenation of parts (from interpolation).

type ExprLiteral

type ExprLiteral struct {
	Value any
}

ExprLiteral is a constant value (string, float64, bool, nil).

type ExprNode

type ExprNode interface {
	// contains filtered or unexported methods
}

ExprNode is the interface for all expression AST nodes.

func ParseExpression

func ParseExpression(src string) (ExprNode, error)

ParseExpression parses an expression string into an AST.

func ParseInterpolation

func ParseInterpolation(text string) (ExprNode, bool)

ParseInterpolation is the exported equivalent of parseInterpolation, for use by the root package tests which cannot call the unexported version.

type ExprRef

type ExprRef struct {
	Path string
}

ExprRef is a dotted reference path like "user.name" or just "count".

type ExprTernary

type ExprTernary struct {
	Cond ExprNode
	Then ExprNode
	Else ExprNode
}

ExprTernary is a ternary conditional (cond ? then : else).

type ExprUnary

type ExprUnary struct {
	Op      UnaryOp
	Operand ExprNode
}

ExprUnary is a unary operation (e.g. !visible, -offset).

type FactoryContext

type FactoryContext struct {
	Fonts           map[string]*sg.FontFamily
	DefaultFont     *sg.FontFamily
	DefaultFontSize float64
	// CustomVariants maps user-defined variant names to their Variant slots.
	// Populated by SetTheme; nil means only built-in names are resolved.
	CustomVariants map[string]widget.Variant
	// contains filtered or unexported fields
}

FactoryContext provides resources needed by component factories.

type IRAttribute

type IRAttribute struct {
	Name    string   // attribute name (e.g. "text", "size")
	Static  string   // static value (empty when Expr is set)
	Expr    ExprNode // parsed expression for bind: attributes
	IsEvent bool     // true for on:click etc.
}

IRAttribute represents a single attribute on an IR node.

type IRDirective

type IRDirective struct {
	Type    DirectiveType
	Expr    ExprNode // condition or collection expression
	VarName string   // loop variable name (for ui:for)
}

IRDirective represents a structural directive attached to an IR node.

type IRNode

type IRNode struct {
	ComponentType string
	Attributes    []IRAttribute
	Children      []*IRNode
	Directives    []IRDirective
	Text          string // interpolated text content
	ThemePatch    []byte // raw JSON from a <Theme> child element (root node only)
}

IRNode is the intermediate representation of a compiled XML template element.

func CompileXML

func CompileXML(data []byte) (*IRNode, error)

CompileXML parses XML template data and compiles it to an IR tree.

func CompileXMLWithTypes

func CompileXMLWithTypes(data []byte, extraTypes map[string]bool) (*IRNode, error)

CompileXMLWithTypes parses XML template data and compiles it to an IR tree, accepting extra custom component type names in addition to built-in types.

func DecodeIR

func DecodeIR(data []byte) (*IRNode, error)

DecodeIR deserializes an IRNode tree from the .xmlui binary format.

type TemplateRegistry

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

TemplateRegistry stores compiled XML templates and instantiates them.

func NewTemplateRegistry

func NewTemplateRegistry() *TemplateRegistry

NewTemplateRegistry creates a new template registry.

func NewTemplateRegistryWithFont

func NewTemplateRegistryWithFont(ttf []byte, size float64) (*TemplateRegistry, error)

NewTemplateRegistryWithFont creates a new template registry with a default font loaded from raw TTF data and a display font size. This is a convenience for the common 3-line boilerplate of NewTemplateRegistry + SetFonts + SetFontSize.

func (*TemplateRegistry) Get

func (r *TemplateRegistry) Get(name string) *IRNode

Get returns the compiled IR for a named template.

func (*TemplateRegistry) Instantiate

func (r *TemplateRegistry) Instantiate(name string, ctrl widget.Controller, screen *widget.Screen) (*widget.Component, error)

Instantiate creates a live component tree from a named template. ctrl may be nil for templates that have no reactive bindings or event handlers.

func (*TemplateRegistry) InstantiateIR

func (r *TemplateRegistry) InstantiateIR(ir *IRNode, ctrl widget.Controller, screen *widget.Screen) (*widget.Component, error)

InstantiateIR creates a live component tree from an IR node directly.

func (*TemplateRegistry) InstantiateStatic

func (r *TemplateRegistry) InstantiateStatic(name string, screen *widget.Screen) (*widget.Component, error)

InstantiateStatic creates a live component tree from a named template without any controller. Use this for templates that have no reactive bindings or event handlers.

func (*TemplateRegistry) RegisterBinary

func (r *TemplateRegistry) RegisterBinary(name string, binData []byte) error

RegisterBinary decodes a .xmlui binary blob and registers the template by name.

func (*TemplateRegistry) RegisterWidget

func (r *TemplateRegistry) RegisterWidget(typeName string, factory WidgetFactory, setters map[string]AttrSetter)

RegisterWidget registers a custom widget type for use in XML templates. The typeName must not collide with built-in widget names. The factory creates the component; setters apply XML attributes by name. RegisterWidget must be called before RegisterXML for any template that uses the custom widget type.

func (*TemplateRegistry) RegisterXML

func (r *TemplateRegistry) RegisterXML(name string, xmlData []byte) error

RegisterXML compiles and registers an XML template by name.

func (*TemplateRegistry) SetFontSize

func (r *TemplateRegistry) SetFontSize(size float64)

SetFontSize configures the default display font size for instantiation.

func (*TemplateRegistry) SetFonts

func (r *TemplateRegistry) SetFonts(fonts map[string]*sg.FontFamily, defaultFont *sg.FontFamily)

SetFonts configures the font map and default font for instantiation.

func (*TemplateRegistry) SetTheme

func (r *TemplateRegistry) SetTheme(t *widget.Theme)

SetTheme registers the compiled theme with the registry so that custom variant names declared in the theme JSON (e.g. "card", "muted") can be resolved when the variant attribute is used in templates.

func (*TemplateRegistry) SetThemeJSON

func (r *TemplateRegistry) SetThemeJSON(data []byte) error

SetThemeJSON registers a theme from raw JSON bytes. The JSON is stored so that inline <Theme> patches in templates can be merged against it.

type UnaryOp

type UnaryOp int

UnaryOp identifies a unary operator.

const (
	UnaryNot UnaryOp = iota
	UnaryNeg
)

type WidgetFactory

type WidgetFactory func(name string) (*widget.Component, error)

WidgetFactory creates a custom widget component by name. The returned component should have UserData set to the typed widget struct so that attribute setters can cast it back.

Jump to

Keyboard shortcuts

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