Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

List of maps in Sightly

Avatar

Avatar
Validate 25
Level 3
alistairp781078
Level 3

Likes

10 likes

Total Posts

96 posts

Correct Reply

5 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 10
Give Back 5
View profile

Avatar
Validate 25
Level 3
alistairp781078
Level 3

Likes

10 likes

Total Posts

96 posts

Correct Reply

5 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 10
Give Back 5
View profile
alistairp781078
Level 3

21-07-2017

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?

View Entire Topic

Avatar

Avatar
Boost 5
Level 4
zeeshank1500736
Level 4

Likes

28 likes

Total Posts

103 posts

Correct Reply

33 solutions
Top badges earned
Boost 5
Boost 3
Boost 25
Boost 10
Boost 1
View profile

Avatar
Boost 5
Level 4
zeeshank1500736
Level 4

Likes

28 likes

Total Posts

103 posts

Correct Reply

33 solutions
Top badges earned
Boost 5
Boost 3
Boost 25
Boost 10
Boost 1
View profile
zeeshank1500736
Level 4

21-07-2017

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