Search code examples
groovyhandlebars.jshandlebarshelperhandlebars.java

Use handlebars Java to iterate over a map of maps


I am currently creating a small Ratpack, Java, Groovy project. I am using Handlebars Java as my templating system.

https://github.com/jknack/handlebars.java

I am attempting to iterate over a map that has a key and then a map as a value. This is an example of my data.

[manOfTheMatches:[Forlan:20], cleanSheets:[Rooney:30]]

I want to iterate over each item in my map and then iterate over the map within each value to display for example.

"Most man of the matches Forlan with 20"

At the moment I am doing this.

   {{#each mostMotm}}
        <div class="col-sm-4 col-xl-3">
            <div class="panel panel-tile text-center br-a br-grey">
                <div class="panel-body">

                    <h6 class="text-success">Most {{@key}}</h6>
                </div>
                <div class="panel-footer br-t p12">
              <span class="fs11">
                <i class="fa fa-arrow-up pr5"></i>
                <b>{{this}}</b>
              </span>
                </div>
            </div>
        </div>
    {{/each}}

@key correctly prints out the key however 'this' just displays Forlan:20. I tried to loop over the inside map but had no luck, has anyone come across this before?


Solution

  • I got around this by looping over the inside map using this below.

     {{#each this}}
            {{this}}
        {{/each}}
    

    my code now looks like :

    {{#each mostMotm}}
                <div class="col-sm-4 col-xl-3">
                    <div class="panel panel-tile text-center br-a br-grey">
                        <div class="panel-body">
                            {{#each this}}
                                <h1 class="fs30 mt5 mbn">{{this}}</h1>
                            {{/each}}
                            <h6 class="text-success">Most {{@key}}</h6>
                        </div>
    {{/each}}