Skip to content

Commit 07f39af

Browse files
russ4stallarouel
authored andcommitted
Fix Handlebars template rendering (see bigskysoftware#801 and bigskysoftware#1801)
1 parent ec126d2 commit 07f39af

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

src/ext/client-side-templates.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,27 @@ htmx.defineExtension('client-side-templates', {
2828
var handlebarsTemplate = htmx.closest(elt, "[handlebars-template]");
2929
if (handlebarsTemplate) {
3030
var data = JSON.parse(text);
31-
var templateName = handlebarsTemplate.getAttribute('handlebars-template');
32-
return Handlebars.partials[templateName](data);
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+
}
3339
}
3440

3541
var handlebarsArrayTemplate = htmx.closest(elt, "[handlebars-array-template]");
3642
if (handlebarsArrayTemplate) {
3743
var data = JSON.parse(text);
38-
var templateName = handlebarsArrayTemplate.getAttribute('handlebars-array-template');
39-
return Handlebars.partials[templateName]({"data": data});
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+
}
4052
}
4153

4254
var nunjucksTemplate = htmx.closest(elt, "[nunjucks-template]");
@@ -50,7 +62,7 @@ htmx.defineExtension('client-side-templates', {
5062
return nunjucks.render(templateName, data);
5163
}
5264
}
53-
65+
5466
var xsltTemplate = htmx.closest(elt, "[xslt-template]");
5567
if (xsltTemplate) {
5668
var templateId = xsltTemplate.getAttribute('xslt-template');

test/ext/client-side-templates.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ describe("client-side-templates extension", function() {
2929
it('works on basic handlebars template', function () {
3030
this.server.respondWith("GET", "/test", '{"foo":"bar"}');
3131
var btn = make('<button hx-get="/test" hx-ext="client-side-templates" handlebars-template="hb1">Click Me!</button>')
32-
Handlebars.partials["hb1"] = Handlebars.compile("*{{foo}}*");
32+
make('<script id="hb1" type="text/x-handlebars-template">*{{foo}}*</script>')
3333
btn.click();
3434
this.server.respond();
3535
btn.innerHTML.should.equal("*bar*");
3636
});
3737

3838
it('works on handlebars array template', function () {
39-
this.server.respondWith("GET", "/test", '{"foo":"bar"}');
39+
this.server.respondWith("GET", "/test", '[{"foo":"bar"}]');
4040
var btn = make('<button hx-get="/test" hx-ext="client-side-templates" handlebars-array-template="hb1">Click Me!</button>')
41-
Handlebars.partials["hb1"] = Handlebars.compile("*{{data.foo}}*");
41+
make('<script id="hb1" type="text/x-handlebars-template">*{{#.}}{{foo}}{{/.}}*</script>')
4242
btn.click();
4343
this.server.respond();
4444
btn.innerHTML.should.equal("*bar*");
@@ -56,4 +56,4 @@ describe("client-side-templates extension", function() {
5656
this.server.respond();
5757
btn.innerHTML.should.equal("*bar*");
5858
});
59-
});
59+
});

www/content/extensions/client-side-templates.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ When you add this extension on an element, any element below it in the DOM can u
1515
the template the standard way for that template engine:
1616

1717
* `mustache` - looks a mustache &lt;script> tag up by ID for the template content
18-
* `handlebars` - looks in the `Handlebars.partials` collection for a template with that name
18+
* `handlebars` - looks a handlebars &lt;script> tag up by ID for the template content
1919
* `nunjucks` - resolves the template by name via `nunjucks.render(<template-name>)
2020
* `xslt` - looks an XSLT &lt;script> tag up by ID for the template content
2121

0 commit comments

Comments
 (0)