Skip to content

Commit cb59a8a

Browse files
committed
Add handlebars.java as template engine
1 parent 1d13795 commit cb59a8a

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

modules/jooby-bom/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<properties>
1616
<!-- Parser and Renderer -->
1717
<freemarker.version>2.3.28</freemarker.version>
18+
<handlebars.version>4.1.2</handlebars.version>
1819
<jackson.version>2.9.6</jackson.version>
1920

2021
<!-- logging -->
@@ -170,6 +171,13 @@
170171
<version>${freemarker.version}</version>
171172
</dependency>
172173

174+
<!-- Handlebars -->
175+
<dependency>
176+
<groupId>com.github.jknack</groupId>
177+
<artifactId>handlebars</artifactId>
178+
<version>${handlebars.version}</version>
179+
</dependency>
180+
173181
<!-- Logging System -->
174182
<dependency>
175183
<groupId>org.slf4j</groupId>

modules/jooby-handlebars/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
<version>${jooby.version}</version>
2626
</dependency>
2727

28-
<!-- Freemarker -->
28+
<!-- Handlebars -->
2929
<dependency>
30-
<groupId>org.freemarker</groupId>
31-
<artifactId>freemarker</artifactId>
30+
<groupId>com.github.jknack</groupId>
31+
<artifactId>handlebars</artifactId>
3232
</dependency>
3333

3434
<!-- Test dependencies -->
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.jooby.handlebars;
2+
3+
import com.github.jknack.handlebars.Handlebars;
4+
import com.github.jknack.handlebars.Template;
5+
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
6+
import com.github.jknack.handlebars.io.TemplateLoader;
7+
import io.jooby.Context;
8+
import io.jooby.ModelAndView;
9+
import io.jooby.TemplateEngine;
10+
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
public class Hbs implements TemplateEngine {
16+
17+
private Handlebars handlebars;
18+
19+
public Hbs(Handlebars handlebars) {
20+
this.handlebars = handlebars;
21+
}
22+
23+
public Hbs() {
24+
this(build(new ClassPathTemplateLoader("/views", "")));
25+
}
26+
27+
public static Handlebars build(TemplateLoader loader) {
28+
Handlebars handlebars = new Handlebars(loader);
29+
handlebars.setCharset(StandardCharsets.UTF_8);
30+
return handlebars;
31+
}
32+
33+
@Override public String apply(Context ctx, ModelAndView modelAndView) throws Exception {
34+
Template template = handlebars.compile(modelAndView.view);
35+
Map<String, Object> model = new HashMap<>(ctx.locals());
36+
model.putAll(modelAndView.model);
37+
return template.apply(model);
38+
}
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.jooby.handlebars;
2+
3+
import io.jooby.MockContext;
4+
import io.jooby.ModelAndView;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class HbsTest {
10+
public static class User {
11+
private String firstname;
12+
13+
private String lastname;
14+
15+
public User(String firstname, String lastname) {
16+
this.firstname = firstname;
17+
this.lastname = lastname;
18+
}
19+
20+
public String getFirstname() {
21+
return firstname;
22+
}
23+
24+
public String getLastname() {
25+
return lastname;
26+
}
27+
}
28+
29+
@Test
30+
public void render() throws Exception {
31+
Hbs engine = new Hbs();
32+
String output = engine
33+
.apply(new MockContext().set("local", "var"), new ModelAndView("index.hbs")
34+
.put("user", new User("foo", "bar"))
35+
.put("sign", "!"));
36+
assertEquals("Hello foo bar var!\n", output);
37+
}
38+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello {{user.firstname}} {{user.lastname}} {{local}}{{sign}}

modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<module>server</module>
1818
<module>jooby-jackson</module>
1919
<module>jooby-freemarker</module>
20+
<module>jooby-handlebars</module>
2021
</modules>
2122

2223
<dependencyManagement>

0 commit comments

Comments
 (0)