From 1e66a221e0dc00cedae2a94286c85a426cdbfe84 Mon Sep 17 00:00:00 2001 From: loveuer Date: Fri, 25 Oct 2024 10:23:52 +0800 Subject: [PATCH] feat: add RenderHTML, Redirect --- ctx.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ctx.go b/ctx.go index 1e19ccb..6133b76 100644 --- a/ctx.go +++ b/ctx.go @@ -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) }