Skip to content

Commit 02943b6

Browse files
committed
AMD precompile resolves partials inconsistent with Java implementation fix #550
1 parent bc445ec commit 02943b6

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

handlebars-maven-plugin/src/test/resources/issue234.expected.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ define('a.hbs', ['handlebars'], function(Handlebars) {
66
+ "!";
77
},"useData":true});
88
var templates = Handlebars.templates = Handlebars.templates || {};
9-
templates['a.hbs'] = template;
9+
templates['a'] = template;
1010
var partials = Handlebars.partials = Handlebars.partials || {};
11-
partials['a.hbs'] = template;
11+
partials['a'] = template;
1212
return template;
1313
});
1414

@@ -20,9 +20,9 @@ define('c.hbs', ['handlebars'], function(Handlebars) {
2020
+ "!";
2121
},"useData":true});
2222
var templates = Handlebars.templates = Handlebars.templates || {};
23-
templates['c.hbs'] = template;
23+
templates['c'] = template;
2424
var partials = Handlebars.partials = Handlebars.partials || {};
25-
partials['c.hbs'] = template;
25+
partials['c'] = template;
2626
return template;
2727
});
2828

handlebars/src/main/java/com/github/jknack/handlebars/helper/PrecompileHelper.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ public void tail(final StringBuilder buffer) {
7474
buffer.append(" return template;\n");
7575
buffer.append("});");
7676
}
77+
78+
@Override public void registerTemplate(final StringBuilder buffer, final String name,
79+
final String function) {
80+
String templateName = name.substring(0, name.lastIndexOf('.'));
81+
super.registerTemplate(buffer, templateName, function);
82+
}
7783
},
7884

7985
/**
@@ -114,7 +120,7 @@ public void tail(final StringBuilder buffer) {
114120
public void registerTemplate(final StringBuilder buffer, final String name,
115121
final String function) {
116122
buffer.append("\n var template = Handlebars.template(").append(function).append(");\n");
117-
String[] namespaces = {"templates", "partials" };
123+
String[] namespaces = {"templates", "partials"};
118124
String separator = ";\n";
119125
for (String namespace : namespaces) {
120126
buffer.append(" var ").append(namespace).append(" = Handlebars.").append(namespace)
@@ -137,7 +143,7 @@ public CharSequence wrap(final String name, final String function) {
137143
registerTemplate(buffer, name, function);
138144
tail(buffer);
139145
return buffer;
140-
};
146+
}
141147

142148
/**
143149
* Find the a wrap strategy.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.github.jknack.handlebars.issues;
2+
3+
import com.github.jknack.handlebars.v4Test;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
8+
public class Issue550 extends v4Test {
9+
@Test
10+
public void precompileAMDShouldNotAddSuffixToTemplatePartialHash() throws IOException {
11+
shouldCompileTo("{{precompile 'foo' wrapper='amd'}}{{precompile 'bar' wrapper='amd'}}",
12+
$("partials", $(
13+
"foo", "<div>Some repeated pattern {{#each listItem}} {{> bar}} {{/each}} </div>",
14+
"bar", "<div>{{text}}</div>")),
15+
"define('foo.hbs', ['handlebars'], function(Handlebars) {\n"
16+
+ " var template = Handlebars.template({\"compiler\":[7,\">= 4.0.0\"],\"main\":function(container,depth0,helpers,partials,data) {\n"
17+
+ " var stack1;\n"
18+
+ "\n"
19+
+ " return \"<div>Some repeated pattern \"\n"
20+
+ " + ((stack1 = helpers.each.call(depth0 != null ? depth0 : {},(depth0 != null ? depth0.listItem : depth0),{\"name\":\"each\",\"hash\":{},\"fn\":container.program(1, data, 0),\"inverse\":container.noop,\"data\":data})) != null ? stack1 : \"\")\n"
21+
+ " + \" </div>\";\n"
22+
+ "},\"1\":function(container,depth0,helpers,partials,data) {\n"
23+
+ " var stack1;\n"
24+
+ "\n"
25+
+ " return \" \"\n"
26+
+ " + ((stack1 = container.invokePartial(partials.bar,depth0,{\"name\":\"bar\",\"data\":data,\"helpers\":helpers,\"partials\":partials,\"decorators\":container.decorators})) != null ? stack1 : \"\")\n"
27+
+ " + \" \";\n"
28+
+ "},\"usePartial\":true,\"useData\":true});\n"
29+
+ " var templates = Handlebars.templates = Handlebars.templates || {};\n"
30+
+ " templates['foo'] = template;\n"
31+
+ " var partials = Handlebars.partials = Handlebars.partials || {};\n"
32+
+ " partials['foo'] = template;\n"
33+
+ " return template;\n"
34+
+ "});define('bar.hbs', ['handlebars'], function(Handlebars) {\n"
35+
+ " var template = Handlebars.template({\"compiler\":[7,\">= 4.0.0\"],\"main\":function(container,depth0,helpers,partials,data) {\n"
36+
+ " var helper;\n"
37+
+ "\n"
38+
+ " return \"<div>\"\n"
39+
+ " + container.escapeExpression(((helper = (helper = helpers.text || (depth0 != null ? depth0.text : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === \"function\" ? helper.call(depth0 != null ? depth0 : {},{\"name\":\"text\",\"hash\":{},\"data\":data}) : helper)))\n"
40+
+ " + \"</div>\";\n"
41+
+ "},\"useData\":true});\n"
42+
+ " var templates = Handlebars.templates = Handlebars.templates || {};\n"
43+
+ " templates['bar'] = template;\n"
44+
+ " var partials = Handlebars.partials = Handlebars.partials || {};\n"
45+
+ " partials['bar'] = template;\n"
46+
+ " return template;\n"
47+
+ "});");
48+
}
49+
}

handlebars/src/test/resources/amd.precompiled.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ define('input.hbs', ['handlebars'], function(Handlebars) {
55
+ "!";
66
},"useData":true});
77
var templates = Handlebars.templates = Handlebars.templates || {};
8-
templates['input.hbs'] = template;
8+
templates['input'] = template;
99
var partials = Handlebars.partials = Handlebars.partials || {};
10-
partials['input.hbs'] = template;
10+
partials['input'] = template;
1111
return template;
1212
});

0 commit comments

Comments
 (0)