The goal of this library is to quickly add some reflection based functional functions to the text/template and
html/template language. Namely:
mapfilterfindfindIndex
This library exists in lieu of generic support in text/template or html/template.
func MapTemplateFunc(slice any, f any) (any, error)The map function.func FilterTemplateFunc(slice any, f any) (any, error)The map function.func TextFunctions() text/template.FuncMapfunc HtmlFunctions() html/template.FuncMap
func TextFunctions() tt.FuncMap {
return map[string]any{
"filter": FilterTemplateFunc,
"find": FindTemplateFunc,
"findIndex": FindIndexTemplateFunc,
"map": MapTemplateFunc,
}
}
func HtmlFunctions() ht.FuncMap {
return map[string]any{
"filter": FilterTemplateFunc,
"find": FindTemplateFunc,
"findIndex": FindIndexTemplateFunc,
"map": MapTemplateFunc,
}
}In go: MapTemplateFunc, provided as map by TextFunctions and HtmlFunctions
Definition:
func MapTemplateFunc(slice any, f any) (any, error)
- The first argument
slicemust be a slice (or nil), of any type. - Second argument
fmust be a function of these definitions:func () anyfunc () (any, error)func (v any) anyfunc (v any) (any, error)
The return will be:
- The first result: an array of the same length as
slicein the case, or nil if there was an error. The output offwill be in the appropriate place for each value - The 2nd result: an error if there was an error: See errors.go for a complete list.
Usage:
{{ map $.Data $.Funcs.inc }}{{ map $.Data $.Funcs.odd }}
In go: FilterTemplateFunc, provided as filter by TextFunctions and HtmlFunctions
Definition:
func FilterTemplateFunc(slice any, f any) (any, error)
- The first argument
slicemust be a slice (or nil), of any type. - Second argument
fmust be a function of these definitions:func () boolfunc () (bool, error)func (v any) boolfunc (v any) (bool, error)
The return will be:
- The first result: an array of the same length or smaller as
slicein the case, or nil if there was an error. The output offwill be a slice which only contains the values whichfreturned true forslice - The 2nd result: an error if there was an error: See errors.go for a complete list.
Usage:
{{ filter $.Data $.Funcs.odd }}
In go: FindTemplateFunc, provided as find by TextFunctions and HtmlFunctions
Definition:
func FindTemplateFunc(slice any, f any) (any, error)
- The first argument
slicemust be a slice (or nil), of any type. - Second argument
fmust be a function of these definitions:func () boolfunc () (bool, error)func (v any) boolfunc (v any) (bool, error)
The return will be:
- The first matching value, or nil
- The 2nd result: an error if there was an error: See errors.go for a complete list.
Usage:
{{ filter $.Data $.Funcs.odd }}
In go: FindIndexTemplateFunc, provided as findIndex by TextFunctions and HtmlFunctions
Definition:
func FindIndexTemplateFunc(slice any, f any) (int, error)
- The first argument
slicemust be a slice (or nil), of any type. - Second argument
fmust be a function of these definitions:func () boolfunc () (bool, error)func (v any) boolfunc (v any) (bool, error)
The return will be:
- The first matching index, or -1
- The 2nd result: an error if there was an error: See errors.go for a complete list.
Usage:
{{ filter $.Data $.Funcs.odd }}
package main
import (
"fmt"
"github.com/arran4/go-template-functional-operations"
"github.com/arran4/go-template-functional-operations/misc"
"os"
"text/template"
)
func main() {
funcs := map[string]any{
"inc": func(i int) int {
return i + 1
},
"odd": func(i int) bool {
return i%2 == 1
},
}
funcs = misc.MergeMaps(funtemplates.TextFunctions(), funcs)
tmpl := template.Must(template.New("").Funcs(funcs).Parse(`
{{ map $.Data $.Funcs.inc }}
{{ map $.Data $.Funcs.odd }}
`))
data := struct {
Data []int
Funcs map[string]any
}{
Data: []int{1, 2, 3, 4},
Funcs: funcs,
}
err := tmpl.Execute(os.Stdout, data)
if err != nil {
fmt.Println(err)
}
}Returns:
[2 3 4 5]
[true false true false]