47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package pages
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/ekzyis/echo-htmx-templ-tailwindcss/env"
|
|
"github.com/ekzyis/echo-htmx-templ-tailwindcss/pages/components"
|
|
pCtx "github.com/ekzyis/echo-htmx-templ-tailwindcss/pages/context"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func GetEnv(ctx context.Context) string {
|
|
if u, ok := ctx.Value(pCtx.Env).(string); ok {
|
|
return u
|
|
}
|
|
return "development"
|
|
}
|
|
|
|
func Render(t templ.Component, statusCode int, eCtx echo.Context) error {
|
|
buf := templ.GetBuffer()
|
|
defer templ.ReleaseBuffer(buf)
|
|
|
|
rCtx := context.WithValue(eCtx.Request().Context(), pCtx.Env, env.Env)
|
|
req := eCtx.Request()
|
|
rCtx = context.WithValue(rCtx, pCtx.Origin, req.Pattern+req.Host)
|
|
|
|
if err := t.Render(rCtx, buf); err != nil {
|
|
return err
|
|
}
|
|
|
|
return eCtx.HTML(statusCode, buf.String())
|
|
}
|
|
|
|
func RenderModal(child templ.Component, statusCode int, eCtx echo.Context) error {
|
|
buf := templ.GetBuffer()
|
|
defer templ.ReleaseBuffer(buf)
|
|
|
|
rCtx := templ.WithChildren(eCtx.Request().Context(), child)
|
|
|
|
if err := components.Modal(true).Render(rCtx, buf); err != nil {
|
|
return err
|
|
}
|
|
|
|
return eCtx.HTML(statusCode, buf.String())
|
|
}
|