chtml

package
v0.0.0-...-a543c70 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrComponentNotFound = errors.New("component not found")

ErrComponentNotFound is returned by Importer implementations when a component is not found.

Functions

func AnyPlusAny

func AnyPlusAny(a any, b any) any

func Interpol

func Interpol(s string, args map[string]any) (*vm.Program, error)

Interpol converts a string with ${}-style placeholders to meta program. If the string is a simple text with no interpolation, it returns (nil, nil). If args is not nil, the expression engine will do type checking.

Types

type Component

type Component interface {
	Render(s Scope) (*RenderResult, error)
}

func Parse

func Parse(r io.Reader, imp Importer) (Component, error)

Parse parses the CHTML component from the given reader into a suitable representation for rendering.

func ParseFile

func ParseFile(fsys fs.FS, fname string, imp Importer) (Component, error)

ParseFile parses the CHTML component from the given file. Unlike Parse, it may also watch for changes in the file and trigger a re-parse when necessary.

type Importer

type Importer interface {
	Import(name string) (Component, error)
}

Importer defines an interface for importing components, providing a method to retrieve a component provider by name. The component provider is a function that instantiates components, typically called during CHTML template parsing. For efficiency, the Import method can be invoked once at app initialization, assuming all components are known, and caching is handled to prevent redundant loads.

type ImporterFunc

type ImporterFunc func(name string) (Component, error)

ImporterFunc allows ordinary functions to act as Importer instances.

func (ImporterFunc) Import

func (f ImporterFunc) Import(name string) (Component, error)

Import implements the Importer interface, delegating the call to the ImporterFunc itself.

type RenderResult

type RenderResult struct {
	Header     http.Header
	HTML       *html.Node
	StatusCode int
	Data       any
}

type Scope

type Scope interface {
	// Parent returns the parent scope of the current scope or nil if it's the root scope.
	Parent() Scope

	// Spawn creates a new child scope with extra variables added to it.
	Spawn(vars map[string]any) Scope

	// Vars returns all variables in the scope.
	Vars() map[string]any

	// Closed returns a channel that is closed when the scope is not going to be rendered.
	Closed() <-chan struct{}

	// Touch marks the scope as changed. The change is propagated to the parent scopes.
	Touch()
}

Scope defines an interface for managing arguments in a CHTML component. The CHTML component creates a new scope for each loop iteration, conditional branch, and component import with Spawn method.

type ScopeMap

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

ScopeMap is a simple implementation of the Scope interface based on map[string]any type and suitable to work with expr-lang's env. This implementation copies the variables from the parent scope to the child scope.

func NewScopeMap

func NewScopeMap(parent Scope) *ScopeMap

func (*ScopeMap) Closed

func (s *ScopeMap) Closed() <-chan struct{}

func (*ScopeMap) Parent

func (s *ScopeMap) Parent() Scope

Parent returns the parent scope of the current scope or nil if it's the root scope.

func (*ScopeMap) SetVars

func (s *ScopeMap) SetVars(vars map[string]any)

SetVars replaces internal variables with the given map.

func (*ScopeMap) Spawn

func (s *ScopeMap) Spawn(vars map[string]any) Scope

Spawn creates a new child scope of the current scope with its own set of arguments.

func (*ScopeMap) Touch

func (s *ScopeMap) Touch()

func (*ScopeMap) Vars

func (s *ScopeMap) Vars() map[string]any

Jump to

Keyboard shortcuts

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