numerus/pkg/template.go

42 lines
1.0 KiB
Go

package pkg
import (
"html/template"
"io"
"net/http"
)
func templateFile(name string) string {
return "web/template/" + name
}
func mustRenderTemplate(wr io.Writer, r *http.Request, layout string, filename string, data interface{}) {
locale := getLocale(r)
company := getCompany(r)
t := template.New(filename)
t.Funcs(template.FuncMap{
"gettext": locale.Get,
"pgettext": locale.GetC,
"companyURI": func(uri string) string {
if company == nil {
return uri
}
return "/company/" + company.Slug + uri
},
})
if _, err := t.ParseFiles(templateFile(filename), templateFile(layout)); err != nil {
panic(err)
}
if err := t.ExecuteTemplate(wr, layout, data); err != nil {
panic(err)
}
}
func mustRenderAppTemplate(w io.Writer, r *http.Request, filename string, data interface{}) {
mustRenderTemplate(w, r, "app.gohtml", filename, data)
}
func mustRenderWebTemplate(w io.Writer, r *http.Request, filename string, data interface{}) {
mustRenderTemplate(w, r, "web.gohtml", filename, data)
}