feat: add RenderHTML, Redirect

This commit is contained in:
loveuer 2024-10-25 10:23:52 +08:00
parent df318682fa
commit be161614fb

16
ctx.go
View File

@ -8,6 +8,7 @@ import (
"fmt"
"github.com/google/uuid"
"github.com/loveuer/nf/internal/sse"
"html/template"
"io"
"mime/multipart"
"net"
@ -362,6 +363,21 @@ func (c *Ctx) HTML(html string) error {
return err
}
func (c *Ctx) RenderHTML(name, html string, obj any) error {
c.SetHeader("Content-Type", "text/html")
t, err := template.New(name).Parse(html)
if err != nil {
return err
}
return t.Execute(c.Writer, obj)
}
func (c *Ctx) Redirect(url string, code int) error {
http.Redirect(c.Writer, c.Request, url, code)
return nil
}
func (c *Ctx) Write(data []byte) (int, error) {
return c.Writer.Write(data)
}