nichady / golte

Render Svelte components in your Go http handlers.
https://pkg.go.dev/github.com/nichady/golte
MIT License
201 stars 7 forks source link

Feature Request: Add RenderComponent Method #6

Closed ezynda3 closed 8 months ago

ezynda3 commented 9 months ago

I have a bit of an uncommon usecase but would love to be able to use this as my main templating system in conjunction with HTMX. As such it would be great to be able to render simple components without the need for a layout.

ezynda3 commented 9 months ago

Right now I have a hacky workaround for anyone interested.

component := func(component string, props map[string]any) echo.HandlerFunc {
    return func(c echo.Context) error {
        golte.GetRenderContext(c.Request()).Components = nil

    golte.RenderPage(c.Response().Writer, c.Request(), component, props)
    return nil
    }
}
ezynda3 commented 9 months ago

Just realized there's no way to shed the template as well so you'll just end up returning the component wrapped with the original html template...

nichady commented 8 months ago

I've never used HTMX, but you can set the code inside the html template to this:

{{ .Head }}
{{ .Body }}

and that won't add any boilerplate HTML. Does this solve your use case?

ezynda3 commented 8 months ago

Yes. Thanks!