45 lines
1.1 KiB
Go
45 lines
1.1 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,
|
|
"currentLocale": func() string {
|
|
return locale.Language.String()
|
|
},
|
|
"companyURI": func(uri string) string {
|
|
if company == nil {
|
|
return uri
|
|
}
|
|
return "/company/" + company.Slug + uri
|
|
},
|
|
})
|
|
if _, err := t.ParseFiles(templateFile(filename), templateFile(layout), templateFile("form.gohtml")); 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)
|
|
}
|