mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 09:23:09 +00:00
Add path functions for use in templates
This commit is contained in:
parent
db44d5fcf8
commit
29795fa5b1
34
config.go
34
config.go
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
htemplate "html/template"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
|
@ -62,16 +63,13 @@ func LoadConfig(path string) (*Config, error) {
|
|||
URLs []string
|
||||
}
|
||||
|
||||
// Initialize templates
|
||||
c.templates = NewTemplates()
|
||||
c.templates.Funcs(map[string]interface{}{
|
||||
"site": func() Site {
|
||||
funcs["site"] = func() Site {
|
||||
return Site{
|
||||
Title: c.Title,
|
||||
URLs: c.URLs,
|
||||
}
|
||||
},
|
||||
"partial": func(name string, data interface{}) (interface{}, error) {
|
||||
}
|
||||
funcs["partial"] = func(name string, data interface{}) (interface{}, error) {
|
||||
t, ok := c.templates.FindPartial(name)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("Error: partial %q not found", name)
|
||||
|
@ -81,13 +79,29 @@ func LoadConfig(path string) (*Config, error) {
|
|||
return "", err
|
||||
}
|
||||
return b.String(), nil
|
||||
},
|
||||
}
|
||||
|
||||
// Initialize templates
|
||||
c.templates = NewTemplates()
|
||||
c.templates.Funcs(funcs)
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
var funcs = map[string]interface{}{
|
||||
"safeHTML": func(s string) htemplate.HTML { return htemplate.HTML(s) },
|
||||
"safeHTMLAttr": func(s string) htemplate.HTMLAttr { return htemplate.HTMLAttr(s) },
|
||||
"safeCSS": func(s string) htemplate.CSS { return htemplate.CSS(s) },
|
||||
"safeJS": func(s string) htemplate.JS { return htemplate.JS(s) },
|
||||
"safeURL": func(s string) htemplate.URL { return htemplate.URL(s) },
|
||||
})
|
||||
|
||||
return c, nil
|
||||
"path": func() map[string]interface{} { return pathFuncs },
|
||||
}
|
||||
|
||||
var pathFuncs = map[string]interface{}{
|
||||
"Base": path.Base,
|
||||
"Clean": path.Clean,
|
||||
"Dir": path.Dir,
|
||||
"Ext": path.Ext,
|
||||
"Join": path.Join,
|
||||
"Split": path.Split,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue