From 727e714eb182e67d1dd7cfd5858572de807cdf01 Mon Sep 17 00:00:00 2001 From: symphony-thibault Date: Mon, 12 Oct 2020 16:29:50 +0200 Subject: [PATCH] APP-3120 Handlebars does not automatically append .hbs extension to template name --- .../template/handlebars/HandlebarsEngine.java | 16 +++++++++--- .../handlebars/HandlebarsEngineTest.java | 26 +++++++++++++++++-- .../src/test/resources/base.hbs | 10 +++++++ .../src/test/resources/home.hbs | 5 ++++ 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 symphony-bdk-template/symphony-bdk-template-handlebars/src/test/resources/base.hbs create mode 100644 symphony-bdk-template/symphony-bdk-template-handlebars/src/test/resources/home.hbs diff --git a/symphony-bdk-template/symphony-bdk-template-handlebars/src/main/java/com/symphony/bdk/template/handlebars/HandlebarsEngine.java b/symphony-bdk-template/symphony-bdk-template-handlebars/src/main/java/com/symphony/bdk/template/handlebars/HandlebarsEngine.java index b2440d68d..b02b8c008 100644 --- a/symphony-bdk-template/symphony-bdk-template-handlebars/src/main/java/com/symphony/bdk/template/handlebars/HandlebarsEngine.java +++ b/symphony-bdk-template/symphony-bdk-template-handlebars/src/main/java/com/symphony/bdk/template/handlebars/HandlebarsEngine.java @@ -5,6 +5,7 @@ import com.symphony.bdk.template.api.TemplateException; import com.github.jknack.handlebars.Handlebars; +import com.github.jknack.handlebars.io.ClassPathTemplateLoader; import com.github.jknack.handlebars.io.FileTemplateLoader; import com.github.jknack.handlebars.io.TemplateLoader; import org.apache.commons.io.FilenameUtils; @@ -23,7 +24,7 @@ public class HandlebarsEngine implements TemplateEngine { /** Handlebars for classpath loading. Ok for thread-safety. */ - private static final Handlebars HANDLEBARS = new Handlebars(); + private static final Handlebars HANDLEBARS = createHandlebars(new ClassPathTemplateLoader()); /** * {@inheritDoc} @@ -33,8 +34,7 @@ public Template newTemplateFromFile(String templatePath) { final String basedir = FilenameUtils.getFullPathNoEndSeparator(templatePath); final String file = FilenameUtils.getName(templatePath); // for thread-safety, we need to create a specific Handlebars object - final TemplateLoader templateLoader = new FileTemplateLoader(basedir); - final Handlebars handlebars = new Handlebars(templateLoader); + final Handlebars handlebars = createHandlebars(new FileTemplateLoader(basedir)); try { return new HandlebarsTemplate(handlebars.compile(file)); } catch (IOException e) { @@ -53,4 +53,14 @@ public Template newTemplateFromClasspath(String templatePath) { throw new TemplateException("Unable to compile Handlebars template from classpath location: " + templatePath, e); } } + + /** + * Creates a new {@link Handlebars} object with suffix set to "" to make this {@link TemplateEngine} implementation + * consistent with other ones (e.g. developers have to specify the template resource extension). + */ + private static Handlebars createHandlebars(final TemplateLoader templateLoader) { + final Handlebars handlebars = new Handlebars(templateLoader); + handlebars.getLoader().setSuffix(""); + return handlebars; + } } diff --git a/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/java/com/symphony/bdk/template/handlebars/HandlebarsEngineTest.java b/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/java/com/symphony/bdk/template/handlebars/HandlebarsEngineTest.java index 8fe3db3fb..69753f089 100644 --- a/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/java/com/symphony/bdk/template/handlebars/HandlebarsEngineTest.java +++ b/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/java/com/symphony/bdk/template/handlebars/HandlebarsEngineTest.java @@ -1,6 +1,7 @@ package com.symphony.bdk.template.handlebars; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import com.symphony.bdk.template.api.Template; @@ -25,19 +26,40 @@ void init() { @Test void should_load_template_from_classpath() { - final Template template = this.engine.newTemplateFromClasspath("/test"); + final Template template = this.engine.newTemplateFromClasspath("/test.hbs"); final String content = template.process(Collections.singletonMap("message", "hello")); assertEquals(EXPECTED_TEST_HBS, content); } + @Test + void should_load_complex_template_from_classpath() { + final Template template = this.engine.newTemplateFromClasspath("/home.hbs"); + final String home = template.process(null); + assertTrue(home.contains("Powered by Handlebars.java")); // which is contained in base.hbs + } + @Test void should_load_template_from_file(@TempDir Path tempDir) throws Exception { final Path templatePath = tempDir.resolve("test.hbs"); Files.copy(this.getClass().getResourceAsStream("/test.hbs"), templatePath); - final Template template = this.engine.newTemplateFromFile(tempDir.resolve("test").toAbsolutePath().toString()); + final Template template = this.engine.newTemplateFromFile(tempDir.resolve("test.hbs").toAbsolutePath().toString()); final String content = template.process(Collections.singletonMap("message", "hello")); assertEquals(EXPECTED_TEST_HBS, content); } + + @Test + void should_load_complex_template_from_file(@TempDir Path tempDir) throws Exception { + + // copy /base.hbs and /home.hbs from classpath to tempDir + Path templatePath = tempDir.resolve("home.hbs"); + Files.copy(this.getClass().getResourceAsStream("/home.hbs"), templatePath); + templatePath = tempDir.resolve("base.hbs"); + Files.copy(this.getClass().getResourceAsStream("/base.hbs"), templatePath); + + final Template template = this.engine.newTemplateFromFile(tempDir.resolve("home.hbs").toAbsolutePath().toString()); + final String home = template.process(null); + assertTrue(home.contains("Powered by Handlebars.java")); // which is contained in base.hbs + } } diff --git a/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/resources/base.hbs b/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/resources/base.hbs new file mode 100644 index 000000000..dc2c5ba84 --- /dev/null +++ b/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/resources/base.hbs @@ -0,0 +1,10 @@ +{{#block "header"}} +

Title

+{{/block}} + +{{#block "content"}} +{{/block}} + +{{#block "footer" }} + Powered by Handlebars.java +{{/block}} diff --git a/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/resources/home.hbs b/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/resources/home.hbs new file mode 100644 index 000000000..9f09cde55 --- /dev/null +++ b/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/resources/home.hbs @@ -0,0 +1,5 @@ +{{#partial "content" }} +

Home page

+{{/partial}} + +{{> base.hbs}}