template

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: MIT Imports: 11 Imported by: 2

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Template

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

Template is the representation of a template.

Example
// Information about the terminal can be obtained using the [pkg/term] package.
colorEnabled := true
termWidth := 14
json := strings.NewReader(heredoc.Doc(`[
		{"number": 1, "title": "One"},
		{"number": 2, "title": "Two"}
	]`))
template := "HEADER\n\n{{range .}}{{tablerow .number .title}}{{end}}{{tablerender}}\nFOOTER"
tmpl := New(os.Stdout, termWidth, colorEnabled)
if err := tmpl.Parse(template); err != nil {
	log.Fatal(err)
}
if err := tmpl.Execute(json); err != nil {
	log.Fatal(err)
}
Output:

HEADER

1  One
2  Two

FOOTER

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) 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳