繁体   English   中英

如何使用 Pebble 模板引擎和 Java 使用 vertx 库渲染 jsonArray 中每个 jsonObject 的所有值?

[英]How can I render all the values of each jsonObject in a jsonArray with Pebble templetes engine and Java with vertx library?

这是我的模板:

 <tbody id="byCountry"> {% for dataFilm in film %} <tr> <td>{{Date}}</td> <td>{{Title}}</td> <td>{{MainActor}}</td> </tr> {% endfor %} </tbody>
但是,我应该在某个地方定义“dataFilm”吗? 这是我的带有顶点的 Java 代码:

 summary.onComplete(jsonObjectAsyncResult -> { if (jsonObjectAsyncResult.succeeded()) { JsonArray summaryArray = jsonObjectAsyncResult.result().getJsonArray("Films"); JsonObject filmInfo = new JsonObject(); for( int i = 0; i<summaryArray.size(); i++){ filmInfo.put("summaryArray",summaryArray).put("Date", summaryArray.getJsonObject(i).getString("Date")).put("Title", summaryArray.getJsonObject(i).getString("Title")).put("MainActor", summaryArray.getJsonObject(i).("MainActor")); engine.render(filmInfo, "webroot/templates/films.peb", res ->{ if (res.succeeded()) { routingContext.response().end(res.result()); } else { routingContext.fail(res.cause()); } });

这里的问题是当我尝试渲染时,只渲染第一部电影或最后一部电影,因为我必须用 engine.render 结束响应......知道我应该怎么做吗?

我不知道卵石模板,但似乎你搞砸了你的处理逻辑,试试这样的

if (jsonObjectAsyncResult.succeeded()) {
        JsonArray summaryArray = jsonObjectAsyncResult.result().getJsonArray("Films");
        List<Map<String, Object>> list = new ArrayList<>();
        for(int i=0; i<summaryArray.size(); i++) {
             list.add(summaryArray.getJsonObject(i).getMap());
        }
        Map<String, Object> templateVars = new HashMap<>();
        templateVars.put("films", list);

        engine.render(templateVars, "webroot/templates/countries.peb", res ->{
                if (res.succeeded()) {
                        routingContext.response().setStatusCode(200).end(res.result());
                } else {
                        routingContext.fail(res.cause());
                }
        });
}

和模板代码:

 <tbody id="byCountry">
 {% for dataFilm in films %}
     <tr>
        <td>{{dataFilm.Date}}</td>
        <td>{{dataFilm.Title}}</td>
        <td>{{dataFilm.MainActor}}</td>                    
    </tr>
{% endfor %}
</tbody>```

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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