Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APP-3120 Handlebars does not automatically append .hbs extension to template name #283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}
Expand All @@ -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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{#block "header"}}
<h1>Title</h1>
{{/block}}

{{#block "content"}}
{{/block}}

{{#block "footer" }}
<span>Powered by Handlebars.java</span>
{{/block}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#partial "content" }}
<p>Home page</p>
{{/partial}}

{{> base.hbs}}