Files
openfsd/web/templates.go
2025-05-12 17:21:16 -07:00

33 lines
642 B
Go

package main
import (
"bytes"
"github.com/gin-gonic/gin"
"html/template"
"io"
"net/http"
"path"
)
var basePath = path.Join(".", "templates")
func loadTemplate(key string) (t *template.Template) {
t, err := template.ParseFiles(path.Join(basePath, "layout.html"), path.Join(basePath, key+".html"))
if err != nil {
panic(err)
}
return
}
func writeTemplate(c *gin.Context, key string, data any) {
c.Writer.Header().Set("Content-Type", "text/html")
buf := bytes.Buffer{}
if err := loadTemplate(key).Execute(&buf, nil); err != nil {
c.Writer.WriteHeader(http.StatusInternalServerError)
return
}
io.Copy(c.Writer, &buf)
}