Implement support for HTML templates

This commit is contained in:
adnano 2021-05-01 14:11:41 -04:00
parent 3743589c38
commit fe4afb0d2b
2 changed files with 26 additions and 9 deletions

View file

@ -5,7 +5,6 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"text/template"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
) )
@ -60,7 +59,7 @@ func (c *Config) LoadTemplates(path string) error {
// Load templates // Load templates
c.Templates = NewTemplates() c.Templates = NewTemplates()
c.Templates.Funcs(template.FuncMap{ c.Templates.Funcs(map[string]interface{}{
"site": func() Site { "site": func() Site {
return Site{ return Site{
Title: c.Title, Title: c.Title,

View file

@ -1,6 +1,8 @@
package main package main
import ( import (
htemplate "html/template"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
pathpkg "path" pathpkg "path"
@ -9,22 +11,27 @@ import (
"text/template" "text/template"
) )
// Template represents a template.
type Template interface {
Execute(io.Writer, interface{}) error
}
// Templates contains site templates. // Templates contains site templates.
type Templates struct { type Templates struct {
tmpls map[string]*template.Template tmpls map[string]Template
funcs template.FuncMap funcs map[string]interface{}
} }
// NewTemplates returns a new Templates with the default templates. // NewTemplates returns a new Templates with the default templates.
func NewTemplates() *Templates { func NewTemplates() *Templates {
t := &Templates{ t := &Templates{
tmpls: map[string]*template.Template{}, tmpls: map[string]Template{},
} }
return t return t
} }
// Funcs sets the functions available to newly created templates. // Funcs sets the functions available to newly created templates.
func (t *Templates) Funcs(funcs template.FuncMap) { func (t *Templates) Funcs(funcs map[string]interface{}) {
t.funcs = funcs t.funcs = funcs
} }
@ -36,6 +43,13 @@ func (t *Templates) LoadTemplate(path string, content []byte) {
t.tmpls[path] = tmpl t.tmpls[path] = tmpl
} }
func (t *Templates) LoadHTMLTemplate(path string, content []byte) {
tmpl := htemplate.New(path)
tmpl.Funcs(htemplate.FuncMap(t.funcs))
htemplate.Must(tmpl.Parse(string(content)))
t.tmpls[path] = tmpl
}
// Load loads templates from the provided directory // Load loads templates from the provided directory
func (t *Templates) Load(dir string) error { func (t *Templates) Load(dir string) error {
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
@ -49,14 +63,18 @@ func (t *Templates) Load(dir string) error {
} }
// Remove directory from beginning of path // Remove directory from beginning of path
path = strings.TrimPrefix(path, dir) path = strings.TrimPrefix(path, dir)
t.LoadTemplate(path, b) if pathpkg.Ext(path) == ".html" {
t.LoadHTMLTemplate(path, b)
} else {
t.LoadTemplate(path, b)
}
} }
return nil return nil
}) })
} }
// FindTemplate returns the template for the given path. // FindTemplate returns the template for the given path.
func (t *Templates) FindTemplate(path string, tmpl string) (*template.Template, bool) { func (t *Templates) FindTemplate(path string, tmpl string) (Template, bool) {
tmplPath := pathpkg.Join(path, tmpl) tmplPath := pathpkg.Join(path, tmpl)
if t, ok := t.tmpls[tmplPath]; ok { if t, ok := t.tmpls[tmplPath]; ok {
return t, true return t, true
@ -69,7 +87,7 @@ func (t *Templates) FindTemplate(path string, tmpl string) (*template.Template,
} }
// FindPartial returns the partial template of the given name. // FindPartial returns the partial template of the given name.
func (t *Templates) FindPartial(name string) (*template.Template, bool) { func (t *Templates) FindPartial(name string) (Template, bool) {
if t, ok := t.tmpls[pathpkg.Join("/_partials", name)]; ok { if t, ok := t.tmpls[pathpkg.Join("/_partials", name)]; ok {
return t, true return t, true
} }