0

I'm constantly struggling with NoSuchFileException while trying to reach templates for HandleBars template engine for Vertx. Personally i think that ether Vertx file system root is inconsistent or I'm missing something, code snippet is following:

    String templateLocation = "templates"+File.separator+"index.hbs";
    fs = vertx.fileSystem();
    fs.exists(templateLocation, existHandler -> {
        if(existHandler.succeeded() && existHandler.result() == true){
            engine.render(context,templateLocation, renderResult -> {
                if (renderResult.succeeded()) {
                    context.request().response().putHeader("Content-Type", "text/html");
                    context.response().end(renderResult.result());
                } else {
                    context.fail(renderResult.cause());
                }
            });

Firs, I'm confirming via exist, does directory and template file exist. If yes, them start render action on same directory, I'm falling into:

java.nio.file.NoSuchFileException: \emplates\index.hbs

Event though FileSystem claims directory exist. Where do HandleBars expect then to find it's templates? I'm already copy/paste folder templates/index.hbs to all possible locations:

  • Project root
  • src/resources
  • directory where java main is executed

all with no success...

Also please notice missing t in exception, is not a typo, looks like something in the stack is not handling very well paths

Tomas
  • 3,269
  • 3
  • 29
  • 48
  • `\emplates\index.hbs` (missing `t`) is weird, is that a typo, or is there something in the stack that has trouble with backslashes? (not familiar with Vert.x) – Hugues M. Aug 14 '17 at 12:10
  • Good point, noticed that recently. Something is eating out first char in path. I'm adding dummy character at the beginning, process continues to fail – Tomas Aug 14 '17 at 16:38
  • You could use forward slashes instead, java [will deal with them](https://stackoverflow.com/a/19762195/6730571) – Hugues M. Aug 14 '17 at 16:53
  • I already did that in many configurations (actual templates folder location) witch no success – Tomas Aug 15 '17 at 06:34

1 Answers1

0

You're trying to do that the wrong way. Vertx should do that for you:

TemplateEngine engine = HandlebarsTemplateEngine.create();
TemplateHandler handler = TemplateHandler.create(engine);

router.get("/*").handler(handler);

http://vertx.io/docs/vertx-web/java/#_handlebars_template_engine

This will render any template under resources/templates

If you still want to call .render by yourself for some reason, you can do it:

router.get("/").handler(ctx -> {
    engine.render(ctx, "templates/index.hbs", res -> {
        if (res.succeeded()) {
            ctx.response().end(res.result());
        }
    });
});

Again, this will look for your templates under /resources folder

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • 1
    Don't know what to tell you, but this is *definitely* not the case. `resources` would only get used if HandlebarsTemplateEngine was using a classpath-based loader, but it's not -- it's just using the filesystem. Unfortunately, since the loader in HandlebarsTempateEngine is hardcoded, it's very hard to get it to use anything other than just directories relative to your working directory. Frustrating. – Max Jan 21 '18 at 18:59