mirror of
https://github.com/onsonr/hway.git
synced 2025-03-10 13:07:09 +00:00
30 lines
605 B
Go
30 lines
605 B
Go
package context
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func Render(c echo.Context, cmp templ.Component) error {
|
|
// Create a buffer to store the rendered HTML
|
|
buf := &bytes.Buffer{}
|
|
// Render the component to the buffer
|
|
err := cmp.Render(c.Request().Context(), buf)
|
|
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())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.Response().WriteHeader(200)
|
|
return nil
|
|
}
|