alps/template.go

47 lines
948 B
Go
Raw Normal View History

2019-12-02 14:31:00 +00:00
package koushin
import (
"html/template"
"io"
"net/url"
2019-12-02 14:31:00 +00:00
"github.com/labstack/echo/v4"
)
type tmpl struct {
// TODO: add support for multiple themes
2019-12-02 14:31:00 +00:00
t *template.Template
}
func (t *tmpl) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.t.ExecuteTemplate(w, name, data)
}
func loadTemplates(logger echo.Logger, themeName string) (*tmpl, error) {
base, err := template.New("").Funcs(template.FuncMap{
2019-12-03 12:07:25 +00:00
"tuple": func(values ...interface{}) []interface{} {
return values
},
"pathescape": func(s string) string {
return url.PathEscape(s)
},
2019-12-03 12:07:25 +00:00
}).ParseGlob("public/*.html")
if err != nil {
return nil, err
}
theme, err := base.Clone()
if err != nil {
return nil, err
}
if themeName != "" {
logger.Printf("Loading theme \"%s\"", themeName)
if _, err := theme.ParseGlob("public/themes/" + themeName + "/*.html"); err != nil {
return nil, err
}
}
return &tmpl{theme}, err
2019-12-02 14:31:00 +00:00
}