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 ¶
- Variables
- func Contains(substr, str string) (interface{}, error)
- func Default(def, val interface{}) (interface{}, error)
- func Expect(val interface{}, format string) (interface{}, error)
- func Index(args ...reflect.Value) (reflect.Value, error)
- func Ip(val interface{}) (interface{}, error)
- func Ipmask(val interface{}) (interface{}, error)
- func Join(sep string, val reflect.Value) (interface{}, error)
- func Optional(val interface{}, format string) (interface{}, error)
- func Slice(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error)
- func Split(sep string, val interface{}) (interface{}, error)
- type Template
- type TemplateOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 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
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
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 Optional ¶ added in v0.0.2
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
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
Types ¶
type Template ¶
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:
type TemplateOption ¶ added in v0.0.2
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