modweb/templates/build.go
2020-10-25 01:26:01 +02:00

34 lines
683 B
Go

package templates
import (
"bytes"
"git.ddd.rip/ptrcnull/modweb/templates/static"
"github.com/gofiber/fiber"
"log"
"text/template"
)
func Send(ctx *fiber.Ctx, data interface{}, templates ...string) {
tmpl, err := BuildHTML(data, templates...)
if err != nil {
log.Println(err)
return
}
ctx.Type(".html", "utf-8").SendString(tmpl)
}
func BuildHTML(data interface{}, templates ...string) (string, error) {
t := template.New("")
t = template.Must(t.Parse(static.HtmlPage))
for _, tmpl := range templates {
t = template.Must(t.Parse(tmpl))
}
buf := bytes.NewBuffer(nil)
err := t.Execute(buf, data)
if err != nil {
return "", err
}
return buf.String(), nil
}