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

Go Fiber Support / Example? #349

Closed
jacobranson opened this issue Dec 20, 2023 · 3 comments
Closed

Go Fiber Support / Example? #349

jacobranson opened this issue Dec 20, 2023 · 3 comments

Comments

@jacobranson
Copy link

Corresponding ticket on Go Fiber's side: gofiber/template#302

Based on some of the conversation going on over there, it looks like there are a few different approaches to integrating both Templ and Fiber. Since there are already examples of Gin and Chi, maybe one could be added for Fiber?

@a-h
Copy link
Owner

a-h commented Dec 20, 2023

I think that since the Fiber context type implements io.Writer, it should be a case of:

app.Get("/", func(c *fiber.Ctx) error {
  return templConponent("hello").Render(c.Context(), c)
})

But I'm just typing this on a mobile, haven't had chance to try it!

@bastianwegge
Copy link
Contributor

bastianwegge commented Dec 22, 2023

I created a helper that does this:

func Render(c *fiber.Ctx, component templ.Component, options ...func(*templ.ComponentHandler)) error {
	componentHandler := templ.Handler(layout.Main(component))
	for _, o := range options {
		o(componentHandler)
	}
	return adaptor.HTTPHandler(componentHandler)(c)
}

This way you can use your render-function like this, which I found easiest:

app.Get("/", func(c *fiber.Ctx) error {
  return view.Render(c, MyTemplView())
})

/e:
This is also very nice because you can define an error handler like this:

func ErrorHandler(c *fiber.Ctx, err error) error {
	return view.Render(c, pages.ErrorView(err.Error()), templ.WithStatus(http.StatusNotFound))
}

Which results in sending the correct status codes.

@a-h
Copy link
Owner

a-h commented Dec 23, 2023

I think we can consider this closed. We've got a good example now, thanks to @bastianwegge.

@a-h a-h closed this as completed Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants