diff --git a/config.go b/config.go index dac1977..042f84d 100644 --- a/config.go +++ b/config.go @@ -1,8 +1,10 @@ package main import ( + "fmt" "os" "path" + "strings" "text/template" "github.com/BurntSushi/toml" @@ -65,6 +67,17 @@ func (c *Config) LoadTemplates(path string) error { URLs: c.URLs, } }, + "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) + } + var b strings.Builder + if err := t.Execute(&b, data); err != nil { + return "", err + } + return b.String(), nil + }, }) return c.Templates.Load(path) } diff --git a/templates.go b/templates.go index ec031be..35480cf 100644 --- a/templates.go +++ b/templates.go @@ -67,3 +67,11 @@ func (t *Templates) FindTemplate(path string, tmpl string) (*template.Template, // Failed to find template return nil, false } + +// FindPartial returns the partial template of the given name. +func (t *Templates) FindPartial(name string) (*template.Template, bool) { + if t, ok := t.tmpls[pathpkg.Join("/_partials", name)]; ok { + return t, true + } + return nil, false +}