41 lines
862 B
Go
41 lines
862 B
Go
package admin
|
|
|
|
import (
|
|
"bytes"
|
|
"embed"
|
|
"html/template"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed templates/*.html
|
|
var templateFS embed.FS
|
|
|
|
var tmpl *template.Template
|
|
|
|
func init() {
|
|
funcMap := template.FuncMap{
|
|
"lower": strings.ToLower,
|
|
}
|
|
tmpl = template.Must(template.New("").Funcs(funcMap).ParseFS(templateFS, "templates/*.html"))
|
|
}
|
|
|
|
func renderTemplate(w io.Writer, name string, data map[string]any) error {
|
|
return tmpl.ExecuteTemplate(w, name, data)
|
|
}
|
|
|
|
func renderToString(name string, data map[string]any) string {
|
|
var buf bytes.Buffer
|
|
if err := tmpl.ExecuteTemplate(&buf, name, data); err != nil {
|
|
return "<p>Error rendering template</p>"
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
func renderLayout(w io.Writer, title string, content template.HTML) error {
|
|
return tmpl.ExecuteTemplate(w, "layout.html", map[string]any{
|
|
"Title": title,
|
|
"Content": content,
|
|
})
|
|
}
|