handlebars.java icon indicating copy to clipboard operation
handlebars.java copied to clipboard

ClassPathTemplateLoader always add .hbs as the template file extension

Open pilak opened this issue 5 years ago • 2 comments

Hello,

I'm using ClassPathTemplateLoader to load my template. My template are named for instance "template.json" but the path that the loader is attempted to resolve is "template.json.hbs"...

Here's an example of the code I'm using :

final TemplateLoader loader = new ClassPathTemplateLoader("template/requests/", ".json");
final Template queryTemplate = handlebars.compile(loader.resolve("my-request"));

And the file is located in classpath template/requests/my-request.json But I'm facing the error below :

java.io.FileNotFoundException: /template/requests/my-request.json.hbs at com.github.jknack.handlebars.io.URLTemplateLoader.sourceAt(URLTemplateLoader.java:70) at com.github.jknack.handlebars.Handlebars.compile(Handlebars.java:438)

So I'm thinking about an issue, as the suffix part of the constructor is supposed to avoid adding the default extension .hbs

Regards

pilak avatar Apr 05 '19 09:04 pilak

You should be able to just do the following

String prefix = "/template/requests/";
String suffix = ".json";

TemplateLoader templateLoader = new ClassPathTemplateLoader(prefix, suffix);
Handlebars handlebars = new Handlebars(templateLoader);

Template template = handlebars.compile("my-request");
template.apply(context);

Best of luck.

TJReinert avatar May 14 '19 18:05 TJReinert

Ok I'll test that, thank you

pilak avatar May 22 '19 08:05 pilak