2025-03-28 17:51:34 +08:00

53 lines
1.0 KiB
Go

package tpl
import (
"bytes"
"net/http"
"path/filepath"
"strings"
"management/internal/db/model/dto"
)
type TemplateConfig struct {
Root string
Extension string
Layout string
Partial string
}
type HtmlData struct {
IsAuthenticated bool
AuthorizeUser dto.AuthorizeUser
AuthorizeMenus []*dto.OwnerMenuDto
Data any
}
func HTML(w http.ResponseWriter, r *http.Request, tpl string, data map[string]any) {
rndr.HTML(w, r, tpl, data)
}
func (r *render) HTML(w http.ResponseWriter, req *http.Request, tpl string, data map[string]any) {
name := strings.ReplaceAll(tpl, "/", "_")
t, ok := r.templates[name]
if !ok {
http.Error(w, "template is empty", http.StatusInternalServerError)
return
}
hd := r.setDefaultData(req, data)
buf := new(bytes.Buffer)
err := t.ExecuteTemplate(buf, filepath.Base(tpl), hd)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = buf.WriteTo(w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}