template

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2022 License: MIT Imports: 11 Imported by: 2

README

Go Reference DeepSource

Overview

Package template implements a wrapper around the Go standard library text/template

It includes helpers to render the template and several additional functions

Documentation

Overview

Package template implements a wrapper around the Go standard library test/template

It includes helpers to render the template and several additional functions. Additional template functions are exposed as public functions, so it shows up in https://pkg.go.dev/github.com/kellerza/template#pkg-functions

Index

Examples

Constants

This section is empty.

Variables

View Source
var Funcs = map[string]interface{}{
	"optional": Optional,
	"expect":   Expect,
	"ip":       Ip,
	"ipmask":   Ipmask,
	"default":  Default,
	"contains": Contains,
	"index":    Index,
	"join":     Join,
	"slice":    Slice,
	"split":    Split,
}

The following functions will be available when rendering the template.

Function slice & index overwrites the standard Go functions, but is compatible to the standard library

Functions

func Contains added in v0.0.2

func Contains(substr, str string) (interface{}, error)

Test if a string contains a substring

func Default added in v0.0.2

func Default(def, val interface{}) (interface{}, error)

Return a default value if a value is not available

func Expect added in v0.0.2

func Expect(val interface{}, format string) (interface{}, error)

The expect function tests if the input satisfies a certain type. If successful it returns nothing and will not affect your template If the type fails an error will be raised and template execution stopped

Expect can check the following types:

• a simple type: str, string, int

• and IP address with mask IP/mask

• a numeric range 0-100

• a regular expression

func Index added in v0.0.2

func Index(args ...reflect.Value) (reflect.Value, error)

The indexes can either follow the value, or be before the value (supporting pipe) Negative indexes are allowed and will be the offset from the length

Example
a := map[string]string{
	"a": "1",
	"b": "2",
}
idx, _ := Index(reflect.ValueOf(a), reflect.ValueOf("a"))
fmt.Println(idx)
// result: "1"
Output:

func Ip added in v0.0.2

func Ip(val interface{}) (interface{}, error)

Return only the IP address from a value containing an IP/mask

func Ipmask added in v0.0.2

func Ipmask(val interface{}) (interface{}, error)

Return only the mask from a value containing an IP/mask

func Join added in v0.0.2

func Join(sep string, val reflect.Value) (interface{}, error)

Joins an array of values or slice using the specified separator

func Optional added in v0.0.2

func Optional(val interface{}, format string) (interface{}, error)

The optional function takes exactly the sameparameters as the expect function If the value is not supplied, the function will return an empty string If a value was supplied, it should match the logic from the expect function

func Slice added in v0.0.2

func Slice(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error)

Slicing.

slice returns the result of text/template's [slice](https://golang.org/pkg/text/template/#hdr-Functions) if that fails, it attempts an alternative implementation, the the first 2 parameters are indexes followed by the value. Negative indexes are allowed and will be the offset from the length

func Split added in v0.0.2

func Split(sep string, val interface{}) (interface{}, error)

Split a string using the separator

Types

type Template

type Template struct {
	T *tmpl.Template
	// contains filtered or unexported fields
}

func New

func New(name string, options ...TemplateOption) (*Template, error)

Get an instance of Template (text/template's Template with all functions added)

Example
p := []string{"./", "../test_data"}
t, _ := New("test", SearchPath(p...))
vars := map[string]interface{}{
	"a": "a",
}
tname := "tst.tmpl"
fmt.Printf("Rendering %s\n", tname)
res, err := t.ExecuteTemplate(tname, vars)
if err != nil {
	fmt.Printf("%v", err)
	return
}
fmt.Printf("OK\n%s\n", res)
Output:

func (*Template) Execute

func (t *Template) Execute(vars map[string]interface{}) (string, error)

Execute the template

func (*Template) ExecuteTemplate

func (t *Template) ExecuteTemplate(name string, vars map[string]interface{}) (string, error)

Execute a specific template

type TemplateOption added in v0.0.2

type TemplateOption func(*Template) error

func ErrorOnMissingKey added in v0.0.4

func ErrorOnMissingKey() TemplateOption

func SearchPath added in v0.0.2

func SearchPath(paths ...string) TemplateOption

Add paths to search for templates

Jump to

Keyboard shortcuts

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