picgo/corelib/template.go

22 lines
441 B
Go

package corelib
import (
"html/template"
"net/http"
)
func TemplateHandler(w http.ResponseWriter, 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)
}
}