Skip to content

Commit 86fa5f9

Browse files
committed
handlebars: support ValueResolver at module level
- fix #3513
1 parent 3fc0cb5 commit 86fa5f9

File tree

5 files changed

+66
-10
lines changed

5 files changed

+66
-10
lines changed

docs/asciidoc/modules/handlebars.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import io.jooby.handlebars.HandlebarsModule;
2828
install(new HandlebarsModule());
2929
3030
get("/", ctx -> {
31-
return new ModelAndView("index.hbs")
31+
return new MapModelAndView("index.hbs")
3232
.put("name", "Jooby");
3333
});
3434
}
@@ -43,7 +43,7 @@ import io.jooby.handlebars.HandlebarsModule
4343
install(HandlebarsModule())
4444
4545
get("/") {
46-
ModelAndView("index.hbs")
46+
MapModelAndView("index.hbs")
4747
.put("name", "Jooby")
4848
}
4949
}

modules/jooby-handlebars/src/main/java/io/jooby/handlebars/HandlebarsModule.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
import java.nio.file.Files;
1515
import java.nio.file.Path;
1616
import java.nio.file.Paths;
17+
import java.util.Deque;
18+
import java.util.LinkedList;
1719
import java.util.List;
1820
import java.util.Optional;
1921

2022
import com.github.jknack.handlebars.Handlebars;
23+
import com.github.jknack.handlebars.ValueResolver;
2124
import com.github.jknack.handlebars.cache.HighConcurrencyTemplateCache;
2225
import com.github.jknack.handlebars.cache.NullTemplateCache;
2326
import com.github.jknack.handlebars.cache.TemplateCache;
@@ -199,6 +202,9 @@ protected URL getResource(String location) {
199202

200203
private Path templatesPath;
201204

205+
private final Deque<ValueResolver> resolvers =
206+
new LinkedList<>(ValueResolver.defaultValueResolvers());
207+
202208
/**
203209
* Creates a new handlebars module.
204210
*
@@ -233,6 +239,17 @@ public HandlebarsModule() {
233239
this(TemplateEngine.PATH);
234240
}
235241

242+
/**
243+
* Add custom value resolver.
244+
*
245+
* @param resolver Value resolver.
246+
* @return This module.
247+
*/
248+
public HandlebarsModule with(@NonNull ValueResolver resolver) {
249+
resolvers.addFirst(resolver);
250+
return this;
251+
}
252+
236253
@Override
237254
public void install(@NonNull Jooby application) throws Exception {
238255
if (handlebars == null) {
@@ -242,7 +259,8 @@ public void install(@NonNull Jooby application) throws Exception {
242259
.setTemplatesPath(templatesPath)
243260
.build(application.getEnvironment());
244261
}
245-
application.encoder(new HandlebarsTemplateEngine(handlebars, EXT));
262+
application.encoder(
263+
new HandlebarsTemplateEngine(handlebars, resolvers.toArray(new ValueResolver[0]), EXT));
246264

247265
ServiceRegistry services = application.getServices();
248266
services.put(Handlebars.class, handlebars);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Handlebars templates for jooby:
3+
*
4+
* <pre>
5+
* import io.jooby.handlebars.HandlebarsModule;
6+
*
7+
* {
8+
* install(new HandlebarsModule());
9+
*
10+
* get("/", ctx -> {
11+
* return new MapModelAndView("index.hbs")
12+
* .put("name", "Jooby");
13+
* });
14+
* }
15+
* </pre>
16+
*
17+
* <p>views/index.hbs
18+
*
19+
* <pre>
20+
* Hello {{name}}!
21+
* </pre>
22+
*
23+
* @author edgar
24+
*/
25+
package io.jooby.handlebars;

modules/jooby-handlebars/src/main/java/io/jooby/internal/handlebars/HandlebarsTemplateEngine.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
*/
66
package io.jooby.internal.handlebars;
77

8-
import static com.github.jknack.handlebars.Context.newContext;
9-
108
import java.util.Collections;
119
import java.util.List;
1210

1311
import com.github.jknack.handlebars.Handlebars;
12+
import com.github.jknack.handlebars.ValueResolver;
1413
import edu.umd.cs.findbugs.annotations.NonNull;
1514
import io.jooby.Context;
1615
import io.jooby.ModelAndView;
@@ -19,11 +18,14 @@
1918

2019
public class HandlebarsTemplateEngine implements TemplateEngine {
2120

22-
private final List<String> extensions;
2321
private final Handlebars handlebars;
22+
private final ValueResolver[] resolvers;
23+
private final List<String> extensions;
2424

25-
public HandlebarsTemplateEngine(Handlebars handlebars, List<String> extensions) {
25+
public HandlebarsTemplateEngine(
26+
Handlebars handlebars, ValueResolver[] resolvers, List<String> extensions) {
2627
this.handlebars = handlebars;
28+
this.resolvers = resolvers;
2729
this.extensions = Collections.unmodifiableList(extensions);
2830
}
2931

@@ -35,7 +37,11 @@ public List<String> extensions() {
3537
@Override
3638
public DataBuffer render(Context ctx, ModelAndView modelAndView) throws Exception {
3739
var template = handlebars.compile(modelAndView.getView());
38-
var engineModel = newContext(modelAndView.getModel()).data(ctx.getAttributes());
40+
var engineModel =
41+
com.github.jknack.handlebars.Context.newBuilder(modelAndView.getModel())
42+
.resolver(resolvers)
43+
.build()
44+
.data(ctx.getAttributes());
3945
var buffer = ctx.getBufferFactory().allocateBuffer();
4046
template.apply(engineModel, buffer.asWriter());
4147
return buffer;

modules/jooby-handlebars/src/test/java/io/jooby/handlebars/HandlebarsModuleTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.junit.jupiter.api.Test;
1515

1616
import com.github.jknack.handlebars.Handlebars;
17+
import com.github.jknack.handlebars.ValueResolver;
1718
import com.typesafe.config.ConfigFactory;
1819
import io.jooby.Environment;
1920
import io.jooby.ModelAndView;
@@ -46,7 +47,10 @@ public void render() throws Exception {
4647
HandlebarsModule.create()
4748
.build(new Environment(getClass().getClassLoader(), ConfigFactory.empty()));
4849
HandlebarsTemplateEngine engine =
49-
new HandlebarsTemplateEngine(handlebars, Arrays.asList(".hbs"));
50+
new HandlebarsTemplateEngine(
51+
handlebars,
52+
ValueResolver.defaultValueResolvers().toArray(new ValueResolver[0]),
53+
Arrays.asList(".hbs"));
5054
MockContext ctx = new MockContext();
5155
ctx.getAttributes().put("local", "var");
5256
var output =
@@ -63,7 +67,10 @@ public void renderFileSystem() throws Exception {
6367
.setTemplatesPath(Paths.get("src", "test", "resources", "views").toString())
6468
.build(new Environment(getClass().getClassLoader(), ConfigFactory.empty()));
6569
HandlebarsTemplateEngine engine =
66-
new HandlebarsTemplateEngine(handlebars, Arrays.asList(".hbs"));
70+
new HandlebarsTemplateEngine(
71+
handlebars,
72+
ValueResolver.defaultValueResolvers().toArray(new ValueResolver[0]),
73+
Arrays.asList(".hbs"));
6774
MockContext ctx = new MockContext();
6875
ctx.getAttributes().put("local", "var");
6976
var output =

0 commit comments

Comments
 (0)