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?
Solved! Go to Solution.
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
Here an example that I wrote on using a HashMap:
htl-examples/HashMapExample.java at master · heervisscher/htl-examples · GitHub
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
should you not have: ${list[map]['title']}
Views
Replies
Total Likes
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
Perfect! This is just what I was after.
Thanks both for your help.
Views
Replies
Total Likes
Views
Likes
Replies