gohtmxgo-echo

Echo Golang render content and change url path without reloading layout page


I am trying to build a SPA.

There are some requirements I am desperately trying to achieve. I have an index.html file as a layout which has some templates defined. The rendering of that works as expected through htmx calls.

The issues I am facing are the following:

  1. I want to enable routes to dynamic content e.g.: I have a list with items. When clicking on an item I want to render a template that shows more information on that item. In general that works but now I also want the URL to change to .../item/:id without re-rendering the entire page
  2. I also want to be able to access said item information through the link directly and have the layout(index.html) rendering as well as I need it for loading styles and scripts

I researched a lot and tried various different things. I also switched from using gin to echo now as I hoped for the integrated rendering to take care of that stuff but without any luck so far


Solution

  • I went with a workaround as I seem to have reached the limitations of go templ. While injecting content into a rendered template is possible, I faced the issue, that when trying to reach .../item/:id without previously visiting .../items/ the other page contents are not being rendered.

    So I stored the data that is being displayed in a session and extract it when redirecting and rendering the new page. That way the user does not notice the reload of the contents that should remain the same when redirecting as I can preload the data from the session storage into the template.