mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
Implement support for partial templates
This commit is contained in:
parent
ff1e4eb29d
commit
3743589c38
13
config.go
13
config.go
|
@ -1,8 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
|
@ -65,6 +67,17 @@ func (c *Config) LoadTemplates(path string) error {
|
||||||
URLs: c.URLs,
|
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)
|
return c.Templates.Load(path)
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,3 +67,11 @@ func (t *Templates) FindTemplate(path string, tmpl string) (*template.Template,
|
||||||
// Failed to find template
|
// Failed to find template
|
||||||
return nil, false
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue