0

This is my template :

 <tbody id="byCountry">
         {% for dataFilm in film %}
             <tr>
                <td>{{Date}}</td>
                <td>{{Title}}</td>
                <td>{{MainActor}}</td>                    
            </tr>
        {% endfor %}
        </tbody>
But, should I define "dataFilm" in somewhere? This is my Java code with vertx :
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());
                        }
                 });

The problem here is when I've tried to render, only rendered the first film or the last film because I have to end the response with the engine.render... Any idea that how I should to do it?

mononoke83
  • 342
  • 2
  • 16

1 Answers1

0

I don't know pebble templates, but seems you messed up your processing logic, try something like this

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());
                }
        });
}

and template code:

 <tbody id="byCountry">
 {% for dataFilm in films %}
     <tr>
        <td>{{dataFilm.Date}}</td>
        <td>{{dataFilm.Title}}</td>
        <td>{{dataFilm.MainActor}}</td>                    
    </tr>
{% endfor %}
</tbody>```
Didar Burmaganov
  • 640
  • 1
  • 8
  • 22
  • Thanks Didar, but I've got the same problem, in the output I see the films size but empty fills. It seems there is some problem with the map of the attributes from the array maybe in the Pebble engine side, I am not sure – mononoke83 Jun 22 '20 at 10:04
  • check this https://github.com/vert-x3/vertx-web/issues/1251 PR marked as milestone for vert.x 4, i think you should go with plain Java List + Java Map, i'll edit my answer – Didar Burmaganov Jun 22 '20 at 12:43