1 | htmx.defineExtension('client-side-templates', {
|
2 | transformResponse : function(text, xhr, elt) {
|
3 |
|
4 | var mustacheTemplate = htmx.closest(elt, "[mustache-template]");
|
5 | if (mustacheTemplate) {
|
6 | var data = JSON.parse(text);
|
7 | var templateId = mustacheTemplate.getAttribute('mustache-template');
|
8 | var template = htmx.find("#" + templateId);
|
9 | if (template) {
|
10 | return Mustache.render(template.innerHTML, data);
|
11 | } else {
|
12 | throw "Unknown mustache template: " + templateId;
|
13 | }
|
14 | }
|
15 |
|
16 | var mustacheArrayTemplate = htmx.closest(elt, "[mustache-array-template]");
|
17 | if (mustacheArrayTemplate) {
|
18 | var data = JSON.parse(text);
|
19 | var templateId = mustacheArrayTemplate.getAttribute('mustache-array-template');
|
20 | var template = htmx.find("#" + templateId);
|
21 | if (template) {
|
22 | return Mustache.render(template.innerHTML, {"data": data });
|
23 | } else {
|
24 | throw "Unknown mustache template: " + templateId;
|
25 | }
|
26 | }
|
27 |
|
28 | var handlebarsTemplate = htmx.closest(elt, "[handlebars-template]");
|
29 | if (handlebarsTemplate) {
|
30 | var data = JSON.parse(text);
|
31 | var templateId = handlebarsTemplate.getAttribute('handlebars-template');
|
32 | var templateElement = htmx.find('#' + templateId).innerHTML;
|
33 | var renderTemplate = Handlebars.compile(templateElement);
|
34 | if (renderTemplate) {
|
35 | return renderTemplate(data);
|
36 | } else {
|
37 | throw "Unknown handlebars template: " + templateId;
|
38 | }
|
39 | }
|
40 |
|
41 | var handlebarsArrayTemplate = htmx.closest(elt, "[handlebars-array-template]");
|
42 | if (handlebarsArrayTemplate) {
|
43 | var data = JSON.parse(text);
|
44 | var templateId = handlebarsArrayTemplate.getAttribute('handlebars-array-template');
|
45 | var templateElement = htmx.find('#' + templateId).innerHTML;
|
46 | var renderTemplate = Handlebars.compile(templateElement);
|
47 | if (renderTemplate) {
|
48 | return renderTemplate(data);
|
49 | } else {
|
50 | throw "Unknown handlebars template: " + templateId;
|
51 | }
|
52 | }
|
53 |
|
54 | var nunjucksTemplate = htmx.closest(elt, "[nunjucks-template]");
|
55 | if (nunjucksTemplate) {
|
56 | var data = JSON.parse(text);
|
57 | var templateName = nunjucksTemplate.getAttribute('nunjucks-template');
|
58 | var template = htmx.find('#' + templateName);
|
59 | if (template) {
|
60 | return nunjucks.renderString(template.innerHTML, data);
|
61 | } else {
|
62 | return nunjucks.render(templateName, data);
|
63 | }
|
64 | }
|
65 |
|
66 | var xsltTemplate = htmx.closest(elt, "[xslt-template]");
|
67 | if (xsltTemplate) {
|
68 | var templateId = xsltTemplate.getAttribute('xslt-template');
|
69 | var template = htmx.find("#" + templateId);
|
70 | if (template) {
|
71 | var content = template.innerHTML ? new DOMParser().parseFromString(template.innerHTML, 'application/xml')
|
72 | : template.contentDocument;
|
73 | var processor = new XSLTProcessor();
|
74 | processor.importStylesheet(content);
|
75 | var data = new DOMParser().parseFromString(text, "application/xml");
|
76 | var frag = processor.transformToFragment(data, document);
|
77 | return new XMLSerializer().serializeToString(frag);
|
78 | } else {
|
79 | throw "Unknown XSLT template: " + templateId;
|
80 | }
|
81 | }
|
82 |
|
83 | var nunjucksArrayTemplate = htmx.closest(elt, "[nunjucks-array-template]");
|
84 | if (nunjucksArrayTemplate) {
|
85 | var data = JSON.parse(text);
|
86 | var templateName = nunjucksArrayTemplate.getAttribute('nunjucks-array-template');
|
87 | var template = htmx.find('#' + templateName);
|
88 | if (template) {
|
89 | return nunjucks.renderString(template.innerHTML, {"data": data});
|
90 | } else {
|
91 | return nunjucks.render(templateName, {"data": data});
|
92 | }
|
93 | }
|
94 | return text;
|
95 | }
|
96 | });
|