diff --git a/examples/integration-gofiber/README.md b/examples/integration-gofiber/README.md
new file mode 100644
index 000000000..12711398c
--- /dev/null
+++ b/examples/integration-gofiber/README.md
@@ -0,0 +1,24 @@
+## Example
+
+This example demonstrates the usage of templ with gofiber.
+
+As soon as you start the server you can access http://localhost:3000/ and see the rendered page.
+
+If you change the URL to http://localhost:3000/john you will see your parameter printed on the page.
+
+This happens both through parameter passing into the templ component and through context using fiber locals.
+
+## Tasks
+
+### build-templ
+
+```
+templ generate
+```
+
+### run
+
+```
+go run .
+```
+
diff --git a/examples/integration-gofiber/go.mod b/examples/integration-gofiber/go.mod
index f8393618e..dd5718406 100644
--- a/examples/integration-gofiber/go.mod
+++ b/examples/integration-gofiber/go.mod
@@ -3,7 +3,7 @@ module github.com/a-h/templ/examples/integration-gofiber
go 1.21
require (
- github.com/a-h/templ v0.2.501
+ github.com/a-h/templ v0.2.747
github.com/gofiber/fiber/v2 v2.52.5
)
diff --git a/examples/integration-gofiber/home.templ b/examples/integration-gofiber/home.templ
index cadbb23cb..9dc7f5dcd 100644
--- a/examples/integration-gofiber/home.templ
+++ b/examples/integration-gofiber/home.templ
@@ -1,7 +1,19 @@
package main
+import (
+ "context"
+)
+
+func NameFromContext(ctx context.Context) string {
+ if name, ok := ctx.Value("name").(string); ok && name != "" {
+ return name
+ }
+ return "World"
+}
+
templ Home(name string) {
Hello { name }
+ Hello { NameFromContext(ctx) } (from context)
}
templ NotFound() {
diff --git a/examples/integration-gofiber/home_templ.go b/examples/integration-gofiber/home_templ.go
index fbbaf1e64..c9545f3b5 100644
--- a/examples/integration-gofiber/home_templ.go
+++ b/examples/integration-gofiber/home_templ.go
@@ -7,6 +7,17 @@ package main
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
+import (
+ "context"
+)
+
+func NameFromContext(ctx context.Context) string {
+ if name, ok := ctx.Value("name").(string); ok && name != "" {
+ return name
+ }
+ return "World"
+}
+
func Home(name string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
@@ -32,13 +43,26 @@ func Home(name string) templ.Component {
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(name)
if templ_7745c5c3_Err != nil {
- return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/integration-gofiber/home.templ`, Line: 4, Col: 18}
+ return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/integration-gofiber/home.templ`, Line: 15, Col: 18}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("")
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Hello ")
+ if templ_7745c5c3_Err != nil {
+ return templ_7745c5c3_Err
+ }
+ var templ_7745c5c3_Var3 string
+ templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(NameFromContext(ctx))
+ if templ_7745c5c3_Err != nil {
+ return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/integration-gofiber/home.templ`, Line: 16, Col: 34}
+ }
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
+ if templ_7745c5c3_Err != nil {
+ return templ_7745c5c3_Err
+ }
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" (from context)
")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@@ -59,9 +83,9 @@ func NotFound() templ.Component {
}()
}
ctx = templ.InitializeContext(ctx)
- templ_7745c5c3_Var3 := templ.GetChildren(ctx)
- if templ_7745c5c3_Var3 == nil {
- templ_7745c5c3_Var3 = templ.NopComponent
+ templ_7745c5c3_Var4 := templ.GetChildren(ctx)
+ if templ_7745c5c3_Var4 == nil {
+ templ_7745c5c3_Var4 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("404
")
diff --git a/examples/integration-gofiber/main.go b/examples/integration-gofiber/main.go
index 82b96a7e0..c66349655 100644
--- a/examples/integration-gofiber/main.go
+++ b/examples/integration-gofiber/main.go
@@ -4,8 +4,6 @@ import (
"github.com/a-h/templ"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
- "github.com/gofiber/fiber/v2/middleware/adaptor"
- "net/http"
)
func main() {
@@ -13,6 +11,7 @@ func main() {
app.Get("/:name?", func(c *fiber.Ctx) error {
name := c.Params("name")
+ c.Locals("name", name)
if name == "" {
name = "World"
}
@@ -24,13 +23,11 @@ func main() {
}
func NotFoundMiddleware(c *fiber.Ctx) error {
- return Render(c, NotFound(), templ.WithStatus(http.StatusNotFound))
+ c.Status(fiber.StatusNotFound)
+ return Render(c, NotFound())
}
-func Render(c *fiber.Ctx, component templ.Component, options ...func(*templ.ComponentHandler)) error {
- componentHandler := templ.Handler(component)
- for _, o := range options {
- o(componentHandler)
- }
- return adaptor.HTTPHandler(componentHandler)(c)
+func Render(c *fiber.Ctx, component templ.Component) error {
+ c.Set("Content-Type", "text/html")
+ return component.Render(c.Context(), c.Response().BodyWriter())
}