44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package template
|
|
|
|
import (
|
|
"html/template"
|
|
"io"
|
|
"net/http"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/locale"
|
|
)
|
|
|
|
func templateFile(name string) string {
|
|
return "web/templates/" + name
|
|
}
|
|
|
|
func MustRender(w io.Writer, locale *locale.Locale, filename string, data interface{}) {
|
|
layout := "layout.gohtml"
|
|
mustRenderLayout(w, locale, layout, filename, data)
|
|
}
|
|
|
|
func mustRenderLayout(w io.Writer, locale *locale.Locale, layout string, filename string, data interface{}) {
|
|
t := template.New(filename)
|
|
t.Funcs(template.FuncMap{
|
|
"gettext": locale.Get,
|
|
"pgettext": locale.GetC,
|
|
"currentLocale": func() string {
|
|
return locale.Language.String()
|
|
},
|
|
})
|
|
if _, err := t.ParseFiles(templateFile(layout), templateFile(filename)); err != nil {
|
|
panic(err)
|
|
}
|
|
if rw, ok := w.(http.ResponseWriter); ok {
|
|
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
}
|
|
if err := t.ExecuteTemplate(w, layout, data); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|