templates

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BuiltinPrefix marks a template source as an embedded built-in template.
	BuiltinPrefix = "builtin:"
)

Variables

This section is empty.

Functions

func Execute

func Execute(tmpl *template.Template, data any) (string, error)

Execute renders tmpl with data and returns the rendered string.

func FuncMap

func FuncMap() template.FuncMap

FuncMap returns the shared template helper functions.

func IsBuiltin

func IsBuiltin(source string) bool

IsBuiltin reports whether source references an embedded built-in template.

func Name

func Name(source string) string

Name trims the builtin prefix and surrounding whitespace from source.

func Parse

func Parse(name, value string, opts ...Option) (*template.Template, error)

Parse parses one template string with the shared function map.

func ParseSource

func ParseSource(templateFS fs.FS, source string, opts ...Option) (*template.Template, error)

ParseSource reads and parses a template from a file path or builtin reference.

func ReadSource

func ReadSource(templateFS fs.FS, source string) (name string, body string, err error)

ReadSource reads a template from a file path or builtin reference.

Types

type MissingKey

type MissingKey string

MissingKey controls how templates behave when a map key is missing.

const (
	// MissingKeyDefault keeps the default text/template behavior and renders "<no value>".
	MissingKeyDefault MissingKey = "default"
	// MissingKeyZero renders the zero value for the missing key's element type.
	MissingKeyZero MissingKey = "zero"
	// MissingKeyError returns an execution error when a key is missing.
	MissingKeyError MissingKey = "error"
)

type Option

type Option func(*options)

Option customizes template parsing.

func WithMissingKey

func WithMissingKey(policy MissingKey) Option

WithMissingKey configures the text/template missingkey option.

type StringTemplate

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

StringTemplate wraps a parsed template that renders strings.

func LoadString

func LoadString(path string, opts ...Option) (*StringTemplate, error)

LoadString reads a file and parses it into a StringTemplate.

func LoadStringFromFS

func LoadStringFromFS(tmplFS fs.FS, path string, opts ...Option) (*StringTemplate, error)

LoadStringFromFS reads a file from an fs.FS into a StringTemplate.

func ParseStringTemplate

func ParseStringTemplate(name, input string, opts ...Option) (*StringTemplate, error)

ParseStringTemplate parses a string-rendering template from a string.

func (*StringTemplate) Render

func (t *StringTemplate) Render(data any) (string, error)

Render executes the template with the provided data and returns a string.

type Template

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

Template wraps a parsed template that renders bytes.

func Load

func Load(path string, opts ...Option) (*Template, error)

Load reads and parses a template file.

func LoadFromFS

func LoadFromFS(tmplFS fs.FS, path string, opts ...Option) (*Template, error)

LoadFromFS reads and parses a template from an fs.FS.

func LoadSource

func LoadSource(templateFS fs.FS, source string, opts ...Option) (*Template, error)

LoadSource reads and parses a template from a file path or builtin reference.

func ParseTemplate

func ParseTemplate(name, input string, opts ...Option) (*Template, error)

ParseTemplate parses a byte-rendering template from a string.

func (*Template) Render

func (t *Template) Render(data any) ([]byte, error)

Render executes the template with the provided data and returns bytes.

type Templates

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

Templates reads built-in templates from an injected filesystem.

func New

func New(files fs.FS) Templates

New creates a built-in template registry backed by files.

The returned registry is used for builtin: template sources. A nil filesystem is allowed, but Read and Exists will return an error and Names will return nil.

func (Templates) Exists

func (t Templates) Exists(source string) error

Exists reports whether the named built-in template can be read.

func (Templates) Names

func (t Templates) Names() []string

Names returns all available built-in template names.

func (Templates) Read

func (t Templates) Read(source string) (filename string, body string, err error)

Read returns an embedded built-in template by name.

Jump to

Keyboard shortcuts

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