1
0
Fork 0
mirror of https://github.com/Luzifer/korvike.git synced 2024-11-08 15:30:05 +00:00
korvike/functions/func_tplexec.go
Knut Ahlers 5a833fa112
Add support for executing sub-templates
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-03-22 16:22:23 +01:00

27 lines
609 B
Go

package functions
import (
"bytes"
"fmt"
"text/template"
)
var subTemplateVariables map[string]interface{}
func SetSubTemplateVariables(m map[string]interface{}) { subTemplateVariables = m }
func init() {
registerFunction("tplexec", func(rawTpl string) (string, error) {
tpl, err := template.New("in").Funcs(GetFunctionMap()).Parse(rawTpl)
if err != nil {
return "", fmt.Errorf("parse template: %w", err)
}
out := new(bytes.Buffer)
if err := tpl.Execute(out, subTemplateVariables); err != nil {
return "", fmt.Errorf("execute template: %w", err)
}
return out.String(), nil
})
}