TemplateEngine { public String build(Object context) throws GenerationException { try { return handlebars .compile(location) .apply(Context.newBuilder(context).combine(model).build()); } catch (IOException e) { throw new GenerationException("Error applying context to template <" + location + ">", e); } } TemplateEngine(String template); TemplateEngine with(String key, Object value); String build(Object context);  }
@Test public void should_load_template_from_classpath() throws GenerationException { TemplateEngine template = new TemplateEngine("template.html"); assertThat(template.build(singletonMap("variable", "foobar"))).isEqualTo("<div>foobar</div>"); }
@Test public void should_allow_using_json_in_templates() throws GenerationException { TemplateEngine template = new TemplateEngine("json-template.html"); Bar bar = new Bar(); bar.setVariable("qux"); assertThat(template.build(singletonMap("variable", bar))).isEqualTo("<div>{\"variable\":\"qux\"}</div>"); }
@Test public void should_allow_using_object_properties_in_templates() throws GenerationException { TemplateEngine template = new TemplateEngine("template.html"); Bar bar = new Bar(); bar.setVariable("baz"); assertThat(template.build(bar)).isEqualTo("<div>baz</div>"); }