aboutsummaryrefslogblamecommitdiff
path: root/template.go
blob: f12e2ecae5627d029b00edf3902913bcb2f6ddc1 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11


               

                       
                 




                                     
                                                


                            



                                                                                         

                                                                         


                                                                    


                                                     
                                     
















                                                                                                    
 
package koushin

import (
	"html/template"
	"io"
	"net/url"

	"github.com/labstack/echo/v4"
)

type tmpl struct {
	// TODO: add support for multiple themes
	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{
		"tuple": func(values ...interface{}) []interface{} {
			return values
		},
		"pathescape": func(s string) string {
			return url.PathEscape(s)
		},
	}).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
}