从 vert.x 渲染时访问车把中的嵌套字段

accessing nested fields in handlebars when rendering from vert.x

我正在尝试使用具有命名字段的对象呈现页面:

{
    "context":{ 
          "greeting":"hello"
     }
}

我用一个非常简单的模板渲染它:

    <html>
    <body>
        <div class="page">
            {{#with context}}
                <h1>{{greeting}} or {{this.greeting}}</h1>
            {{/with}}
                <h1>{{greeting}} or {{context.greeting}}</h1>
        </div>
    <div>the context is actually: {{context}} </div>
    </body>
    </html>

正如您在上面看到的,我目前正在尝试几种呈现 greeting 值的方法。在这个模板的早期版本中,我也一次尝试了一个。

在模板的末尾,我渲染了整个 context 变量,只是为了确保我传入的数据确实存在。这是结果的屏幕截图:

阅读了文档here and a tutorial here我真的看不出我做错了什么,有人可以澄清一下吗?

我应该补充一点,我正在使用 io.vertx:vertx-web-templ-handlebars:3.3.3 来渲染这个

下面是returns这个模板的java方法。我正在使用 Vert.x 渲染引擎。

private void handleStatus(RoutingContext ctx, TemplateEngine engine, String template)
{
    JsonObject json = new JsonObject();
    json.put("greeting", "hello");
    ctx.put("context", json);

    engine.render(ctx, template, res ->
    {
        if (res.succeeded())
        {
            ctx.response().end(res.result());
        }
        else
        {
            ctx.fail(res.cause());
        }
    });
}

这里是调用该方法的地方:

TemplateEngine engine = HandlebarsTemplateEngine.create();
statusHandler = new StatusHandler(vertx);
statusHandler.start();
deploymentHandler = new DeploymentHandler(vertx);

router.get("/monitor/apistatus").handler(ctx -> handleStatus(ctx, engine, "templates/apistatus.hbs"));

在 3.4.0 之前,Vert.x Web Handlebars 模板引擎 did not work well with JsonObject and JsonArray

升级到 3.4/3.5,或者作为解决方法,您可以将 JsonObject 转换为 Map