Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update gofiber example with new rendering #878

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
Loading