template

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package template facilitates processing of JSON strings using Go templates. Provides additional functions not available using basic Go templates, such as coloring, and table rendering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolString added in v0.8.0

func BoolString(v *bool) string

BoolString formats a *bool as a string. Returns "" for nil, "true"/"false" otherwise. Useful as a Go template helper via WithFuncs.

func FormatDuration added in v0.8.0

func FormatDuration(d time.Duration) string

FormatDuration returns a human-readable duration string (e.g. "2m13s").

func HasItems added in v0.8.0

func HasItems(v any) bool

HasItems returns true if v is a non-nil, non-empty slice/array. Accepts a pointer to a slice or a slice directly.

func HasText added in v0.8.0

func HasText(v any) bool

HasText returns true if v is a non-nil, non-whitespace string. For non-string pointer types it returns true if v is non-nil. Useful as a Go template helper via WithFuncs.

func StringOrEmpty added in v0.8.0

func StringOrEmpty(v any) string

StringOrEmpty is a nil-safe string dereference. Accepts any value: returns "" for nil, typed nil pointers, *string, or string; returns the string value otherwise. Useful as a Go template helper via WithFuncs.

func UUIDString added in v0.8.0

func UUIDString(v *uuid.UUID) string

UUIDString formats a *uuid.UUID as a string. Returns "" for nil, the UUID string otherwise. Useful as a Go template helper via WithFuncs.

Types

type Template

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

Template is the representation of a template.

func New

func New(w io.Writer, width int, colorEnabled bool) *Template

New initializes a Template.

func (*Template) Execute

func (t *Template) Execute(input io.Reader) error

Execute applies the parsed template to the input and writes result to the writer the template was initialized with.

func (*Template) ExecuteData

func (t *Template) ExecuteData(v any) error

func (*Template) Flush

func (t *Template) Flush() error

Flush writes any remaining data to the writer. This is mostly useful when a templates uses the tablerow function but does not include the tablerender function at the end. If a template did not use the table functionality this is a noop.

func (*Template) Parse

func (t *Template) Parse(tmpl string) error

Parse the given template string for use with Execute.

func (*Template) WithFuncs

func (t *Template) WithFuncs(funcMap map[string]any) *Template

Funcs adds the elements of the argument map to the template's function map. It must be called before the template is parsed. It is legal to overwrite elements of the map including default functions. The return value is the template, so calls can be chained.

func (*Template) WithTheme

func (t *Template) WithTheme(theme string) *Template

Jump to

Keyboard shortcuts

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