@chg20 I figured it out and I got my example working (https://codepen.io/localhorst/pen/OJyepKG), but I had to modify the extension, to modify my json:
htmx.defineExtension('client-side-templates', {
transformResponse : function(text, xhr, elt) {
var nunjucksTemplate = htmx.closest(elt, "[nunjucks-template]");
if (nunjucksTemplate) {
var data = {}
data.gists = JSON.parse(text);
var templateName = nunjucksTemplate.getAttribute('nunjucks-template');
var template = htmx.find('#' + templateName);
//console.log(templateName,data);
return nunjucks.renderString(template.innerHTML, data);
}
return text;
}
});
What is the right event to modify the json before I use it in the client-side-templates extension? I thought it's afterRequest.htmx
but the changes on evt.detail.xhr
didn't do anything
@chg20 Last thing I was stumbling upon: When I tried to access the gist github api in my example directly, the call got rejected with the message that their CORS rules don't allow X-HX-Current-url
Header, so I tried to reset them:
document.body.addEventListener('configRequest.htmx', function(evt) {
// try to remove x-hx-* headers because gist api compalins about CORS
evt.detail.headers = [];
});
but the current-url header is sent anyway.
Would it be possible to have full control over that?
Object.keys(evt.details.headers).forEach(function(key) { delete object[key]; });
<body script="on configRequest.htmx put [] into detail.headers">
...
</body>
X-HX-Request
header, but I'd like to avoid having to do that on every single request