passing map data-sly-attribute | Community
Skip to main content
Kiran_Vedantam
Community Advisor
Community Advisor
September 30, 2021
Solved

passing map data-sly-attribute

  • September 30, 2021
  • 1 reply
  • 6239 views

I am wanting to generate some "data-attributes" and "values" using a multifield in the dailog.[Ex: data-sample-name = name] How to access those authored values in HTL under single tag?

 

I tried this solution: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/output-html-attribute-name-with-htl/m-p/285013#M63872

but my map from java does not work.

 

Any idea?

 

Thanks,

Kiran Vedantam.

 

 

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 Vijayalakshmi_S

Hi @kiran_vedantam,

Below works fine. Cross check the way you populate the Map. 

If you still face issues, share the source of Map/ multifield content structure.

@Getter
public Map<String, String> requestAttrMap = new HashMap<>();

In @PostConstruct method, populate the map

requestAttrMap.put("attribute01", "Attr Value 01");
requestAttrMap.put("attribute02", "Attr Value 02");
requestAttrMap.put("attribute03", "Attr Value 03");

 In HTL, access like you would access any getter from Sling Model

<div data-sly-use.obj="${'com.aem.demoproject.core.models.ChildResourceCheckModel'>
<!--/* Data attributes */-->
<div data-sly-attribute=${obj.requestAttrMap}>
<p>Data Attributes</p>
</div>
</div>

Update : 

  • Try to create a separate Map with getter  -> populate with hardcoded key-value pairs and see if it works. Then we can narrow down to check the issue with dynamic population using multifield values
  • You can use LinkedHashMap if you would like to display the attributes in the order of insertion. (updated screenshot : used LinkedHashMap + prepended the key with "data-")

1 reply

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
September 30, 2021

Hi @kiran_vedantam,

Below works fine. Cross check the way you populate the Map. 

If you still face issues, share the source of Map/ multifield content structure.

@Getter
public Map<String, String> requestAttrMap = new HashMap<>();

In @PostConstruct method, populate the map

requestAttrMap.put("attribute01", "Attr Value 01");
requestAttrMap.put("attribute02", "Attr Value 02");
requestAttrMap.put("attribute03", "Attr Value 03");

 In HTL, access like you would access any getter from Sling Model

<div data-sly-use.obj="${'com.aem.demoproject.core.models.ChildResourceCheckModel'>
<!--/* Data attributes */-->
<div data-sly-attribute=${obj.requestAttrMap}>
<p>Data Attributes</p>
</div>
</div>

Update : 

  • Try to create a separate Map with getter  -> populate with hardcoded key-value pairs and see if it works. Then we can narrow down to check the issue with dynamic population using multifield values
  • You can use LinkedHashMap if you would like to display the attributes in the order of insertion. (updated screenshot : used LinkedHashMap + prepended the key with "data-")