Add sprig v3 functions to web templates
Signed-off-by: m.nabokikh <maksim.nabokikh@flant.com>
This commit is contained in:
@@ -11,6 +11,8 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/Masterminds/sprig/v3"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -52,6 +54,30 @@ type webConfig struct {
|
||||
extra map[string]string
|
||||
}
|
||||
|
||||
func getFuncMap(c webConfig) (template.FuncMap, error) {
|
||||
funcs := sprig.FuncMap()
|
||||
|
||||
issuerURL, err := url.Parse(c.issuerURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing issuerURL: %v", err)
|
||||
}
|
||||
|
||||
additionalFuncs := map[string]interface{}{
|
||||
"extra": func(k string) string { return c.extra[k] },
|
||||
"issuer": func() string { return c.issuer },
|
||||
"logo": func() string { return c.logoURL },
|
||||
"url": func(reqPath, assetPath string) string {
|
||||
return relativeURL(issuerURL.Path, reqPath, assetPath)
|
||||
},
|
||||
}
|
||||
|
||||
for k, v := range additionalFuncs {
|
||||
funcs[k] = v
|
||||
}
|
||||
|
||||
return funcs, nil
|
||||
}
|
||||
|
||||
// loadWebConfig returns static assets, theme assets, and templates used by the frontend by
|
||||
// reading the dir specified in the webConfig. If directory is not specified it will
|
||||
// use the file system specified by webFS.
|
||||
@@ -113,17 +139,9 @@ func loadTemplates(c webConfig, templatesDir string) (*templates, error) {
|
||||
return nil, fmt.Errorf("no files in template dir %q", templatesDir)
|
||||
}
|
||||
|
||||
issuerURL, err := url.Parse(c.issuerURL)
|
||||
funcs, err := getFuncMap(c)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing issuerURL: %v", err)
|
||||
}
|
||||
|
||||
funcs := map[string]interface{}{
|
||||
"issuer": func() string { return c.issuer },
|
||||
"logo": func() string { return c.logoURL },
|
||||
"url": func(reqPath, assetPath string) string { return relativeURL(issuerURL.Path, reqPath, assetPath) },
|
||||
"lower": strings.ToLower,
|
||||
"extra": func(k string) string { return c.extra[k] },
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tmpls, err := template.New("").Funcs(funcs).ParseFS(c.webFS, filenames...)
|
||||
|
Reference in New Issue
Block a user