picgo/corelib/template.go

22 lines
458 B
Go
Raw Normal View History

2024-07-12 20:32:33 +08:00
package corelib
import (
"html/template"
"net/http"
)
func TemplateHandler(w http.ResponseWriter, r *http.Request, data any, filenames ...string) {
// 解析模板文件
tmpl, err := template.ParseFiles(filenames...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 渲染模板并写入响应
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}