mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-11-27 20:14:20 +00:00
Add path functions for use in templates
This commit is contained in:
parent
66ac6015c9
commit
69f39d7303
34
config.go
34
config.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
htemplate "html/template"
|
htemplate "html/template"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
@ -62,16 +63,13 @@ func LoadConfig(path string) (*Config, error) {
|
||||||
URLs []string
|
URLs []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize templates
|
funcs["site"] = func() Site {
|
||||||
c.templates = NewTemplates()
|
|
||||||
c.templates.Funcs(map[string]interface{}{
|
|
||||||
"site": func() Site {
|
|
||||||
return Site{
|
return Site{
|
||||||
Title: c.Title,
|
Title: c.Title,
|
||||||
URLs: c.URLs,
|
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)
|
t, ok := c.templates.FindPartial(name)
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf("Error: partial %q not found", name)
|
return "", fmt.Errorf("Error: partial %q not found", name)
|
||||||
|
@ -81,13 +79,29 @@ func LoadConfig(path string) (*Config, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return b.String(), nil
|
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) },
|
"safeHTML": func(s string) htemplate.HTML { return htemplate.HTML(s) },
|
||||||
"safeHTMLAttr": func(s string) htemplate.HTMLAttr { return htemplate.HTMLAttr(s) },
|
"safeHTMLAttr": func(s string) htemplate.HTMLAttr { return htemplate.HTMLAttr(s) },
|
||||||
"safeCSS": func(s string) htemplate.CSS { return htemplate.CSS(s) },
|
"safeCSS": func(s string) htemplate.CSS { return htemplate.CSS(s) },
|
||||||
"safeJS": func(s string) htemplate.JS { return htemplate.JS(s) },
|
"safeJS": func(s string) htemplate.JS { return htemplate.JS(s) },
|
||||||
"safeURL": func(s string) htemplate.URL { return htemplate.URL(s) },
|
"safeURL": func(s string) htemplate.URL { return htemplate.URL(s) },
|
||||||
})
|
"path": func() map[string]interface{} { return pathFuncs },
|
||||||
|
}
|
||||||
return c, nil
|
|
||||||
|
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