Partial HTML replies

Hi, I’m playing with htmx and swipl and I think the htmx idiom is to return “partials” as response, parts of the page that then replace already rendered parts/elements.

What I’ve found is that only high-level way to emit html from swipl is to use reply_html_page, but this one seems to emit the whole <html> document with new body tag etc. that breaks the htmx workflow.

My workaround below is not so horribly long, but I’m still wondering if there is a way to do it via reply_html_page?

  format('Content-type: text/html~n~n', []),
  phrase(html_write:html(Doc), Html), 
  html_write:print_html(Html).

Somewhat related: I have a little library sort of thing (based loosely on my understanding of “hotwire”, which I gather is the same idea as htmx?) I use for some of my projects with a similar idiom; what I do instead of sending partials is have the javascript find the matching elements in the response to take out and update the page with; this way it still works the same with javascript disabled.

I haven’t packaged it up, but I have a little project generator that sets up a template to use this setup; you can find that here if you’re interested. Javascript part is here.

The work-around is fine and I’ve been using this code several times when I need an HTML snippet without the DOCTYPE, html, head and body elements. I think it is not for reply_html_page/2 as the page suggests it does the whole thing. We could add this as an alternative predicate. reply_html_fragment/1 ? Maybe reply_html_element/1? Both are a bit long IMO. Better names?

Would reply_html/1 work? It is short and concise.

Fragment already has a meaning in HTML (see Introduction to HTML 4.0) so I’d avoid that. My proposal would be reply_html_partial which is somewhat established terminology for a part of html that is then embedded somewhere else. reply_html works too, but it may be implying that it’s more general than reply_html_page?

1 Like

Added. Its a bit long, but I agree that reply_html can be misleading.

This is a good workaround, and htmx supports it with hx-select.