简体   繁体   中英

Vert.x file root versus Handlebars templates location

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM