mirror of
https://github.com/renorris/openfsd
synced 2026-05-01 14:05:41 +08:00
initial v1.0-beta commit
This commit is contained in:
32
web/templates.go
Normal file
32
web/templates.go
Normal file
@@ -0,0 +1,32 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user