Skip to content

Commit e1a4616

Browse files
committed
precompile / embedded requires helper registration fix #527
1 parent b23ee0a commit e1a4616

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
*/
1818
package com.github.jknack.handlebars.helper;
1919

20+
import com.github.jknack.handlebars.io.TemplateSource;
2021
import static org.apache.commons.lang3.Validate.notEmpty;
2122

2223
import java.io.IOException;
2324

2425
import com.github.jknack.handlebars.Handlebars;
2526
import com.github.jknack.handlebars.Helper;
2627
import com.github.jknack.handlebars.Options;
27-
import com.github.jknack.handlebars.Template;
2828

2929
/**
3030
* Given:
@@ -96,11 +96,11 @@ public Object apply(final String path, final Options options)
9696
String suffix = options.handlebars.getLoader().getSuffix();
9797
String defaultId = (path + suffix).replace('/', '-').replace('.', '-');
9898
String id = options.param(0, defaultId);
99-
Template template = options.handlebars.compile(path);
99+
TemplateSource source = options.handlebars.getLoader().sourceAt(path);
100100
StringBuilder script = new StringBuilder();
101101
script.append("<script id=\"").append(id)
102102
.append("\" type=\"text/x-handlebars\">\n");
103-
script.append(template.text()).append("\n");
103+
script.append(source.content(options.handlebars.getCharset())).append("\n");
104104
script.append("</script>");
105105
return new Handlebars.SafeString(script);
106106
}

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package com.github.jknack.handlebars.helper;
1919

20+
import com.github.jknack.handlebars.internal.JSEngine;
21+
import com.github.jknack.handlebars.io.TemplateSource;
2022
import static org.apache.commons.lang3.Validate.notEmpty;
2123
import static org.apache.commons.lang3.Validate.notNull;
2224

@@ -27,7 +29,6 @@
2729
import com.github.jknack.handlebars.Handlebars;
2830
import com.github.jknack.handlebars.Helper;
2931
import com.github.jknack.handlebars.Options;
30-
import com.github.jknack.handlebars.Template;
3132

3233
/**
3334
* Precompile a template to JavaScript using handlebars.js.
@@ -194,8 +195,10 @@ public Object apply(final String path, final Options options)
194195
if (wrapper == JsWrapper.AMD) {
195196
name += handlebars.getLoader().getSuffix();
196197
}
197-
Template template = handlebars.compile(path);
198-
String precompiled = template.toJavaScript();
198+
TemplateSource source = handlebars.getLoader().sourceAt(path);
199+
String precompiled = JSEngine.RHINO
200+
.toJavaScript(handlebars.handlebarsJsFile(), source.filename(),
201+
source.content(handlebars.getCharset()));
199202
return new Handlebars.SafeString(wrapper.wrap(name, precompiled));
200203
}
201204

handlebars/src/main/java/com/github/jknack/handlebars/internal/JSEngine.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,28 @@
3434
* @author edgar
3535
*
3636
*/
37-
enum JSEngine {
37+
public enum JSEngine {
3838

3939
/**
4040
* The default JS Engine.
4141
*/
4242
RHINO {
4343
@Override
4444
public String toJavaScript(final String hbslocation, final Template template) {
45+
return toJavaScript(hbslocation, template.filename(), template.text());
46+
}
47+
48+
@Override public String toJavaScript(final String hbslocation, final String filename,
49+
final String template) {
4550
Context ctx = null;
4651
try {
4752
ctx = newContext();
4853

4954
Scriptable scope = newScope(hbslocation, ctx);
50-
scope.put("template", scope, template.text());
55+
scope.put("template", scope, template);
5156

5257
String js = "Handlebars.precompile(template);";
53-
Object precompiled = ctx.evaluateString(scope, js, template.toString(), 1,
58+
Object precompiled = ctx.evaluateString(scope, js, filename, 1,
5459
null);
5560

5661
return (String) precompiled;
@@ -127,4 +132,15 @@ private String handlebarsScript(final String location) {
127132
* @return A pre-compiled JavaScript version of this template.
128133
*/
129134
public abstract String toJavaScript(String hbslocation, Template template);
135+
136+
/**
137+
* Convert a template text to JavaScript template (a.k.a precompiled template). Compilation is
138+
* done by handlebars.js and a JS Engine (usually Rhino).
139+
*
140+
* @param hbslocation Location of the handlebars.js file.
141+
* @param filename Template file name.
142+
* @param template The template to convert.
143+
* @return A pre-compiled JavaScript version of this template.
144+
*/
145+
public abstract String toJavaScript(String hbslocation, String filename, String template);
130146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.jknack.handlebars.issues;
2+
3+
import com.github.jknack.handlebars.v4Test;
4+
import static org.junit.Assert.assertEquals;
5+
import org.junit.Test;
6+
7+
public class Issue527 extends v4Test {
8+
9+
@Test
10+
public void precompileShouldNotCompileTemplate() throws Exception {
11+
shouldCompileTo("{{precompile \"client_side\"}}",
12+
$("partials", $("client_side", "{{myhelper \"something\"}}")),
13+
"(function() {\n"
14+
+ " var template = Handlebars.template({\"compiler\":[7,\">= 4.0.0\"],\"main\":function(container,depth0,helpers,partials,data) {\n"
15+
+ " return container.escapeExpression((helpers.myhelper || (depth0 && depth0.myhelper) || helpers.helperMissing).call(depth0 != null ? depth0 : {},\"something\",{\"name\":\"myhelper\",\"hash\":{},\"data\":data}));\n"
16+
+ "},\"useData\":true});\n"
17+
+ " var templates = Handlebars.templates = Handlebars.templates || {};\n"
18+
+ " templates['client_side'] = template;\n"
19+
+ " var partials = Handlebars.partials = Handlebars.partials || {};\n"
20+
+ " partials['client_side'] = template;\n"
21+
+ "})();");
22+
}
23+
24+
@Test
25+
public void embeddedShouldNotCompileTemplate() throws Exception {
26+
shouldCompileTo("{{embedded \"client_side\"}}",
27+
$("partials", $("client_side", "{{myhelper \"something\"}}")),
28+
"<script id=\"client_side-hbs\" type=\"text/x-handlebars\">\n"
29+
+ "{{myhelper \"something\"}}\n"
30+
+ "</script>");
31+
}
32+
}

0 commit comments

Comments
 (0)