Skip to content

Commit f29cc7a

Browse files
bastianweggea-h
andauthoredAug 11, 2024··
chore: update gofiber example with new rendering (#878)
Co-authored-by: Adrian Hesketh <adrianhesketh@hushmail.com>
1 parent 7012baa commit f29cc7a

File tree

5 files changed

+72
-15
lines changed

5 files changed

+72
-15
lines changed
 
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Example
2+
3+
This example demonstrates the usage of templ with gofiber.
4+
5+
As soon as you start the server you can access http://localhost:3000/ and see the rendered page.
6+
7+
If you change the URL to http://localhost:3000/john you will see your parameter printed on the page.
8+
9+
This happens both through parameter passing into the templ component and through context using fiber locals.
10+
11+
## Tasks
12+
13+
### build-templ
14+
15+
```
16+
templ generate
17+
```
18+
19+
### run
20+
21+
```
22+
go run .
23+
```
24+

‎examples/integration-gofiber/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/a-h/templ/examples/integration-gofiber
33
go 1.21
44

55
require (
6-
github.com/a-h/templ v0.2.501
6+
github.com/a-h/templ v0.2.747
77
github.com/gofiber/fiber/v2 v2.52.5
88
)
99

‎examples/integration-gofiber/home.templ

+12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
package main
22

3+
import (
4+
"context"
5+
)
6+
7+
func NameFromContext(ctx context.Context) string {
8+
if name, ok := ctx.Value("name").(string); ok && name != "" {
9+
return name
10+
}
11+
return "World"
12+
}
13+
314
templ Home(name string) {
415
<div>Hello { name }</div>
16+
<div>Hello { NameFromContext(ctx) } (from context)</div>
517
}
618

719
templ NotFound() {

‎examples/integration-gofiber/home_templ.go

+29-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/integration-gofiber/main.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import (
44
"github.com/a-h/templ"
55
"github.com/gofiber/fiber/v2"
66
"github.com/gofiber/fiber/v2/log"
7-
"github.com/gofiber/fiber/v2/middleware/adaptor"
8-
"net/http"
97
)
108

119
func main() {
1210
app := fiber.New()
1311

1412
app.Get("/:name?", func(c *fiber.Ctx) error {
1513
name := c.Params("name")
14+
c.Locals("name", name)
1615
if name == "" {
1716
name = "World"
1817
}
@@ -24,13 +23,11 @@ func main() {
2423
}
2524

2625
func NotFoundMiddleware(c *fiber.Ctx) error {
27-
return Render(c, NotFound(), templ.WithStatus(http.StatusNotFound))
26+
c.Status(fiber.StatusNotFound)
27+
return Render(c, NotFound())
2828
}
2929

30-
func Render(c *fiber.Ctx, component templ.Component, options ...func(*templ.ComponentHandler)) error {
31-
componentHandler := templ.Handler(component)
32-
for _, o := range options {
33-
o(componentHandler)
34-
}
35-
return adaptor.HTTPHandler(componentHandler)(c)
30+
func Render(c *fiber.Ctx, component templ.Component) error {
31+
c.Set("Content-Type", "text/html")
32+
return component.Render(c.Context(), c.Response().BodyWriter())
3633
}

0 commit comments

Comments
 (0)
Please sign in to comment.