I can’t figure out how to preserve a JS expression in an HTML argument in html_write. Here is an example from the HTMX book (Extending HTML As Hypermedia). I want to generate this expression in HTML:
<button hx-get="/contacts" hx-target="#main" hx-swap="outerHTML" hx-trigger="click, keyup[ctrlKey && key == 'L'] from:body">(1)
From the SWI docs (SWI-Prolog -- The HTTP server libraries) it looks to me as if the most promising rule for writing arguments would be:
“”"
Format-Arguments
*:
Use format/3 and emit the result as quoted value.
“”"
So I would expect this to work:
button(['hx-get'(#(contacts_GET)),
'hx-target'('#main'),
'hx-swap'(outerHTML),
'hx-trigger'('click, keyup[ctrlKey && key == "L"] from:body'-[])
],
"Get the Contacts"),
But it always quotes the special characters:
<button hx-get="/contacts" hx-target="#main" hx-swap="outerHTML" hx-trigger="click, keyup[ctrlKey && key == "L"] from:body">Get the Contacts</button>
I tried numerous other variants (too many to remember or list), but couldn’t find anything that worked.
I also played around with \js_call
and \js_script
and found that while js_script
preserves characters, js_call
doesn’t, which seems contrary to the docs:
\js_script("function foo() { return true && !false; }"),
script(language('text/javascript'),\js_call("foo(true && !false)")),
<script type="text/javascript">
function foo() { return true && !false; }
</script>
<script language="text/javascript">
foo(true && !false)();
</script>
Full program: :- use_module(library(http/http_server)).:- use_module(library(http/http_files - Pastebin.com
Where am I going wrong? Is there another syntactic trick I am unaware of?
BTW, HTMX and SWI Prolog backend seems to be a promising combination.