Files
sonr/pkg/nebula/view.go
T

28 lines
587 B
Go
Raw Normal View History

2024-09-24 17:54:33 -04:00
package nebula
2024-09-22 02:51:52 -04:00
import (
"bytes"
2024-09-24 17:54:33 -04:00
"github.com/a-h/templ"
2024-09-22 02:51:52 -04:00
"github.com/labstack/echo/v4"
)
2024-09-24 17:54:33 -04:00
func Render(cmp templ.Component) echo.HandlerFunc {
2024-09-22 02:51:52 -04:00
return func(c echo.Context) error {
// Create a buffer to store the rendered HTML
2024-09-24 17:54:33 -04:00
buf := &bytes.Buffer{}
2024-09-22 02:51:52 -04:00
// Render the component to the buffer
2024-09-24 17:54:33 -04:00
err := cmp.Render(c.Request().Context(), buf)
2024-09-22 02:51:52 -04:00
if err != nil {
return err
}
// Set the content type
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
// Write the buffered content to the response
_, err = c.Response().Write(buf.Bytes())
return err
}
}