Expand my Community achievements bar.

SOLVED

List of maps in Sightly

Avatar

Level 4

Could someone please help me with using a list of maps in Sightly.

I am using a sling model with a List<Map<String, String>> variable and wish to iterate through the list and dynamically pull values from the map based on variable names. For example a map may contain two variables with the following keys: path and title. A list would then comprise one or more of these maps.

I so far have the following HTL code:

<div data-sly-use.articlesearch="xx.xx.xx.xx.core.model.ArticleSearch">

     <div data-sly-list="${articlesearch.ListOfMaps}">

          <div data-sly-list.map="${item}">

               ${map}

               ${mapList.index}

        </div>

    </div>

</div>

This prints out the following:

path 0 title 1

path 0 title 1

...

If I try to access one of the values by its key, e.g. ${map[path]}, I get the following error:

org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Invalid property name

Does anyone have any examples of how to implement this or have some guidance on how I could approach it?

1 Accepted Solution

Avatar

Correct answer by
Level 5

Hi

In your java code LIstOfMaps is a method which is returning like like below sample code.

public List<Map<String, Object>> getValues() {

List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();

if (json != null) {

  for (String value : json) {

          Map<String, Object> column = parseItem(value);

          if (column != null) {

                    results.add(column);

               }

          }

     }

     return results;

}

If this is right then below is the modified Sightly code.
<div data-sly-use.articlesearch="xx.xx.xx.xx.core.model.ArticleSearch">

     <div data-sly-list.listItems="${articlesearch.ListOfMaps}">

       path :  ${listItems['path']}

     title : ${listItems['title']}

    </div>

</div>

Hope this will help !

Thanks

View solution in original post

5 Replies

Avatar

Level 4

Hi Feike,

Thanks, that's a useful example. I've had a go at implementing it with the following code:

<div data-sly-use.articlesearch="xx.xx.xx.xx.core.model.ArticleSearch"

     data-sly-list.list="${articlesearch.ListOfMaps}">

     <ul data-sly-list.map="${list.keySet.iterator}">

          ${map} ${list[map]}

    </ul>

</div>

This prints out all of the variables and their values from the map but only iterates over them in order. What I would be interested in is having some way of pulling out the value for a specified field (e.g. title). Is there any way of doing that?

I've tried ${list[map][title]} but this returns the same error I had in my initial post.

Avatar

Employee

should you not have: ${list[map]['title']}

Avatar

Correct answer by
Level 5

Hi

In your java code LIstOfMaps is a method which is returning like like below sample code.

public List<Map<String, Object>> getValues() {

List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();

if (json != null) {

  for (String value : json) {

          Map<String, Object> column = parseItem(value);

          if (column != null) {

                    results.add(column);

               }

          }

     }

     return results;

}

If this is right then below is the modified Sightly code.
<div data-sly-use.articlesearch="xx.xx.xx.xx.core.model.ArticleSearch">

     <div data-sly-list.listItems="${articlesearch.ListOfMaps}">

       path :  ${listItems['path']}

     title : ${listItems['title']}

    </div>

</div>

Hope this will help !

Thanks

Avatar

Level 4

Perfect! This is just what I was after.

Thanks both for your help.