From 972ef0a20a5a3ca8eae84d2e1d9e18bc4f9ef2d8 Mon Sep 17 00:00:00 2001 From: David Lemaignent Date: Thu, 12 Dec 2024 23:30:58 +0100 Subject: [PATCH] Update client-side-templates.js --- .../client-side-templates.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/client-side-templates/client-side-templates.js b/src/client-side-templates/client-side-templates.js index cf414ff..6ed7280 100644 --- a/src/client-side-templates/client-side-templates.js +++ b/src/client-side-templates/client-side-templates.js @@ -24,6 +24,24 @@ htmx.defineExtension('client-side-templates', { } } + var mustacheHalTemplate = htmx.closest(elt, '[mustache-hal-template]'); + if (mustacheHalTemplate) { + var data = JSON.parse(text); + var templateId = mustacheTemplate.getAttribute('mustache-hal-template'); + var template = htmx.find('#' + templateId); + if (template) { + var partials = {}; + document.querySelectorAll('template[id]').forEach(tpl => { + partials[tpl.id] = tpl.innerHTML; + }); + var renderedContent = Mustache.render(template.innerHTML, data, partials); + renderedContent = renderedContent.replace(//g, ''); // No comments in the Mustache templates ! + return renderedContent; + } else { + throw new Error('Unknown mustache template: ' + templateId); + } + } + var handlebarsTemplate = htmx.closest(elt, '[handlebars-template]') if (handlebarsTemplate) { var data = JSON.parse(text)