Files
schreifuchs.ch/web/layouts/render.go
schreifuchs 812891a205
release.yaml / publish (push) Successful in 3m51s
fix(gallery): use component template
2026-09-29 10:46:45 +02:00

52 lines
1.3 KiB
Go

package layouts
import (
"context"
"io"
"log/slog"
"net/http"
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/templer"
"github.com/a-h/templ"
)
type Layouts struct {
cfg config.Cfg
}
func New(cfg config.Cfg) *Layouts {
return &Layouts{
cfg: cfg,
}
}
// RenderPage renders a page. If the query paramenter HX-Request is set
// the component gets rendered without the layout
func (l *Layouts) RenderPage(ctx context.Context, w io.Writer, r *http.Request, component templ.Component) {
title := templer.GetTitle(ctx, "Schreifuchs")
slog.Debug("HX-Request", "value", r.Header.Get("HX-Request"))
if r.Header.Get("HX-Request") != "true" {
ctx = templ.WithChildren(ctx, component)
component = Base(l.cfg, title)
} else {
ctx = templ.WithChildren(ctx, component)
component = Page(l.cfg, title)
}
if err := component.Render(ctx, w); err != nil {
slog.Error("error while rendering component", "err", err, "ctx", ctx)
}
}
// RenderComponent renders a component alone.
func (l *Layouts) RenderComponent(ctx context.Context, w io.Writer, r *http.Request, component templ.Component) {
ctx = templ.WithChildren(ctx, component)
component = Component()
if err := component.Render(ctx, w); err != nil {
slog.Error("error while rendering component", "err", err, "ctx", ctx)
}
}