Skip to content

Commit

Permalink
chore: update gofiber example with new rendering (#878)
Browse files Browse the repository at this point in the history
Co-authored-by: Adrian Hesketh <[email protected]>
  • Loading branch information
bastianwegge and a-h authored Aug 11, 2024
1 parent 7012baa commit f29cc7a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 15 deletions.
24 changes: 24 additions & 0 deletions examples/integration-gofiber/README.md
Original file line number Diff line number Diff line change
@@ -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 .
```

2 changes: 1 addition & 1 deletion examples/integration-gofiber/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
12 changes: 12 additions & 0 deletions examples/integration-gofiber/home.templ
Original file line number Diff line number Diff line change
@@ -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) {
<div>Hello { name }</div>
<div>Hello { NameFromContext(ctx) } (from context)</div>
}

templ NotFound() {
Expand Down
34 changes: 29 additions & 5 deletions examples/integration-gofiber/home_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions examples/integration-gofiber/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ 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() {
app := fiber.New()

app.Get("/:name?", func(c *fiber.Ctx) error {
name := c.Params("name")
c.Locals("name", name)
if name == "" {
name = "World"
}
Expand All @@ -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())
}

0 comments on commit f29cc7a

Please sign in to comment.