render

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// The function map to use when rendering templates.
	FuncMap = DefaultFuncMap()
)

Functions

func Any added in v0.11.0

func Any(value any, data any) (any, error)

Any attempts to render strings in the given value with data. The input value is assumed to come from an untyped map[string]any (typically from decoding unknown JSON or YAML).

Delegates internally to Map, Slice, and String (see the documentation for those functions for more info). Other types are returned unchanged.

func DefaultFuncMap

func DefaultFuncMap() template.FuncMap

DefaultFuncMap returns a map of functions for the template engine. It includes everything from sprig.FuncMap, plus a number of functions from flect.

func File

func File(path string, data any) (string, error)

File renders the file at path with data.

func Map added in v0.11.0

func Map(values map[string]any, data any) (map[string]any, error)

Map recursively renders the keys and values of the given map with data.

func Slice added in v0.11.0

func Slice(values []any, data any) ([]any, error)

Map recursively renders the elements of the given slice with data.

func String

func String(s string, data any) (string, error)

String renders the template string with data.

Types

type Template

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

Template is a lightweight wrapper for template strings.

Since it implements the encoding.TextMarshaler and encoding.TextUnmarshaler interfaces, it can be used with JSON or YAML fields containing template strings.

func Compile

func Compile(s string) (*Template, error)

Compile parses a template string and returns, if successful, a new Template that can be rendered.

func MustCompile

func MustCompile(s string) *Template

MustCompile behaves like Compile, but panics on error.

func (*Template) MarshalText

func (ts *Template) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Template) Render

func (ts *Template) Render(data any) (string, error)

Render renders the template as a string.

func (*Template) RenderBool added in v0.13.0

func (ts *Template) RenderBool(data any) (bool, error)

Render renders the template as a bool.

func (*Template) RenderInt added in v0.13.0

func (ts *Template) RenderInt(data any) (int, error)

Render renders the template as an int.

func (*Template) RenderRequired added in v0.13.0

func (ts *Template) RenderRequired(data any) (string, error)

RenderRequired renders the template as a string, but returns an error if the result is empty.

func (*Template) UnmarshalText

func (ts *Template) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

Jump to

Keyboard shortcuts

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