Sightly : How to Iterate through Map<String , Map> | Community
Skip to main content
Level 4
December 28, 2016
Solved

Sightly : How to Iterate through Map<String , Map>

  • December 28, 2016
  • 5 replies
  • 36597 views

Backed code submits an object as :

    HashMap<String, HashMap> map= new HashMap<String, HashMap>();

In Sightly how can I access Key, Values of the inner Hash Map.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Feike_Visser1

Here an example the shows how it can be done, without java code.

https://github.com/heervisscher/htl-examples/blob/master/core/src/main/java/com/adobe/examples/htl/core/hashmap/HashMapExample.java

HTL-code:

<div data-sly-use.hashmap="com.adobe.examples.htl.core.hashmap.HashMapExample"
     data-sly-list="${hashmap.map.keySet.iterator}">
     ${item}
     <ul data-sly-list.aem="${hashmap.map[item].keySet.iterator}">
         <li>${aem} ${hashmap.map[item][aem]}</li>
     </ul>
</div>

5 replies

smacdonald2008
Level 10
December 29, 2016

To learn  how to iterate through a collection in HTL (Sightly) -- see this AEM community article: 

https://helpx.adobe.com/experience-manager/using/htl_movie62.html

This will point you in the right direction. 

Runal_Trivedi
Level 6
December 30, 2016

if your hashMap keys are not dynamic, then below is what you can do:

  • you can return iterator on first HashMap through your sightly getter method. 
  • use data-sly-list.outerHash to iterate over the iterator returned value.
    • Each iteration will give you inner HashMap on object outerHash
  • if you know what are going to be the keys, then you can simply access it as outerHash.key1, outerHash,key2 etc.. and it will give you access to values inside the inner hash.

-- Runal

Feike_Visser1
Adobe Employee
Feike_Visser1Adobe EmployeeAccepted solution
Adobe Employee
December 31, 2016

Here an example the shows how it can be done, without java code.

https://github.com/heervisscher/htl-examples/blob/master/core/src/main/java/com/adobe/examples/htl/core/hashmap/HashMapExample.java

HTL-code:

<div data-sly-use.hashmap="com.adobe.examples.htl.core.hashmap.HashMapExample"
     data-sly-list="${hashmap.map.keySet.iterator}">
     ${item}
     <ul data-sly-list.aem="${hashmap.map[item].keySet.iterator}">
         <li>${aem} ${hashmap.map[item][aem]}</li>
     </ul>
</div>

kautuk_sahni
Community Manager
Community Manager
January 2, 2017

Hi 

Wish you a wonderful new year.

HOW TO ITERATE MAP IN HTL(formerly know as sightly)
Suppose you have a Map constructed in your sling model which you want to iterate in HTL, then you can do so using below

Code in Sling Model for constructing map
private Map<Integer,String> map;

public Map<Integer,String> getMap() {
  return map;
 }

@PostConstruct
    public void constructMap() {
        map = new HashMap<Integer, String>();
        map.put(1,"A");
        map.put(2,"B")
    }

Code in HTLfor iterating the HashMap

<sly data-sly-use.model="org.training.ListingModel">
    <ul data-sly-list.key="${model.map}">
         <li> KEY: ${key}, VALUE: ${model.map[key]}</li>
    </ul>
</sly>

Output :- 
A
B

 

Iterate Map of List in HTL:- 
Sometimes due to HTML constraint we have to iterate over a map of list. 

For Example, suppose you have a component which displays a list of products. The products should be displayed in row wise. But the no of products in each row would be varied depending on the total no of products. This can be controlled using JavaScript as well but still you want to implement the logic in Java then you cab create a Map of List.

Code in Sling Model for constructing map
private Map<Integer,List<ProductModel>> map;

private List<ProductModel> products;


public Map<Integer,String> getMap() {
  return map;
 }

@PostConstruct
    public void constructMap() {
    map =  new HashMap<Integer, List<ProductModel>>();

    products = Lists.newArrayList();
    products.add(ele1);
    products.add(ele2);

    map.put(1,products);
       
    }

Code in HTL for iterating the List of HashMap :-
<sly data-sly-use.model="org.training.ListingModel">
<ul data-sly-list.keyName="${model.map}">
        <ul data-sly-list.key="${model.map[keyName]}">
         <li> ${key.productTitle}</li>        
       </ul>
</ul>
</sly>

Output
1
    ele1
    elem2

 

Source Article :- https://cqbasics.blogspot.in/2016/06/iterate-map-and-list-in-sightly-map-of.html

Reference Posts:

Link:- http://stackoverflow.com/a/26148614/6433590

Link:- http://www.wemblog.com/2016/02/how-to-sightly-in-aem-cq.html

I hope this would help you.

~kautuk

Kautuk Sahni
bhargavraj2009
September 7, 2022

Has anyone explored why an Index cannot be used to fetch value from a Map

For example:
<sly data-sly-list="${someList}">

       ${itemList.index} /*Prints the index - 0 to length-1*/

       Consider a Map<int,string> created as

       map.put(0,'Str1')

       map.put(1,'Str2')

 

Now to access the Map, we are forced to iterate the keys

eg: data-sly-list="${map.keySet.iterator}" /*This prints [0,1]*/

map[item] --- results in value

map[0]  --- prints 'Str1'

map[itemList.index] -- from above list prints nothing

whereas ${itemList.index == item} does work

 

/*Is it not a simple solution overlooked*/

</sly>