Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

passing map data-sly-attribute

Avatar

Community Advisor

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...

but my map from java does not work.

 

Any idea?

 

Thanks,

Kiran Vedantam.

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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>

Vijayalakshmi_S_0-1633035143393.png

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-")

Vijayalakshmi_S_0-1633037768267.png

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

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>

Vijayalakshmi_S_0-1633035143393.png

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-")

Vijayalakshmi_S_0-1633037768267.png