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?
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?
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?