summaryrefslogtreecommitdiff
path: root/utils/shortcuts/render.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils/shortcuts/render.go')
-rw-r--r--utils/shortcuts/render.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/utils/shortcuts/render.go b/utils/shortcuts/render.go
new file mode 100644
index 0000000..1efeb61
--- /dev/null
+++ b/utils/shortcuts/render.go
@@ -0,0 +1,38 @@
+package shortcuts
+
+import (
+ "maps"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func Render(ctx *fiber.Ctx, template string, data any) error {
+ bind := make(fiber.Map)
+
+ ctx.Context().VisitUserValues(func(key []byte, value any) {
+ bind[string(key)] = value
+ })
+
+ if data != nil {
+ switch v := data.(type) {
+ case map[string]any:
+ maps.Copy(bind, v)
+ case fiber.Map:
+ maps.Copy(bind, v)
+ default:
+ rv, err := structValue(data)
+ if err != nil {
+ return err
+ }
+
+ maps.Copy(bind, mapStruct(rv))
+ }
+ }
+
+ return ctx.Render(template, bind)
+}
+
+func RenderWithStatus(ctx *fiber.Ctx, template string, data any, statusCode int) error {
+ ctx.Status(statusCode)
+ return Render(ctx, template, data)
+}