Expand my Community achievements bar.

Adobe Summit 2025: AEM Session Recordings Are Live! Missed a session or want to revisit your favorites? Watch the latest recordings now.

data-sly-resource under list iteration

Avatar

Level 2

we have a multifield and we are iterating them in html using list.

in between iteration im using <div data-sly-resource="${'xyz' @ resourceType='prj/components/content/xyz'}"></div>

 

but while authoring ill be getting resource xyz to author at one place not in all places.
and same authorable fields are rendered everywhere.
is it a write behavior?

8 Replies

Avatar

Community Advisor

HI @SudarshanV1 ,

I'd like you t consider few points and then follow the code snippet that I've provided:

  • Ensure that model.items is correctly populated in your Sling Model or script, and each item has a valid resourcePath.
  • The resourcePath should point to a valid resource in the AEM repository that you want to include.
  • You can customize the HTML structure and add additional attributes or elements as needed.

This approach allows you to dynamically include resources for each item in a list, leveraging AEM's powerful resource resolution capabilities.

<!-- Assuming you have a list of items in your model -->
<div data-sly-list="${model.items}">
    <!-- Each item in the list can be accessed using the currentItem variable -->
    <div>
        <!-- Render a resource dynamically for each item -->
        <div data-sly-resource="${currentItem.resourcePath}" />
    </div>
</div>


 -Tarun

Avatar

Level 2

resource is not dynamic. its static in my case and im directly adding it with
<div data-sly-resource="${'xyz' @ resourceType='prj/components/content/xyz'}"></div>

Avatar

Level 4

Hi @SudarshanV1 ,

what i understood from replies is, the 'path' of the resource your are including should be unique per each include in the loop.

ex: <div data-sly-resource="${'xyz' @ resourceType='prj/components/content/xyz'}"></div>

in this case the path is always 'xyz'

instead

<div data-sly-resource="${<some dynamic value or take it from each item> @ resourceType='prj/components/content/xyz'}"></div>

the loop outcome can be

<div data-sly-resource="${itm1 @ resourceType='prj/components/content/xyz'}"></div>

<div data-sly-resource="${itm2 @ resourceType='prj/components/content/xyz'}"></div>

.

.

so that you will get multiple entries of same static 'xyz' component with different paths. (i guess this us what your expectation.)

 

Thanks,

Raju.

Avatar

Level 4

Hi @SudarshanV1 ,

The issue happens because you're using the same resource ('xyz') for each item in the multifield. This makes AEM treat it as a single resource, causing all items to show the same content and not allowing authoring.

To fix the issue, you need to reference a unique resource path for each item in the multifield. This ensures that each item points to its own instance of the component, allowing separate authoring.


<div data-sly-list="${model.items}">
<div data-sly-resource="${item.resourcePath @ resourceType='prj/components/content/xyz'}"></div>
</div>

By doing this, each item in the multifield gets its own unique resource, so it can be authored separately, avoiding the problem of sharing the same resource across all items.

Let me know if it works.
Thanks.

Avatar

Community Advisor

Hi @SudarshanV1 ,

You may refer

<div class="container ">

    <div class="table">
        <sly data-sly-list.row="${count.numberOfRows}">
            <div class="table-row">
                <sly data-sly-list.column="${model.numberOfColumns}">
                    
                    <sly class="table-cell" data-sly-test.resourceNode="cmp-${resource.path.hashCode}-${rowList.count}${columnList.count}"></sly>
                    
                    <div data-sly-resource="${ resourceNode,
                    resourceType='example/components/content/tableLongForm/rte'}"
                    data-sly-unwrap="${!wcmmode.edit && !wcmmode.preview}" style="min-width:60px;">
                    </div>
                
                </sly>
            </div>
        </sly>
    </div>
</div>
Here resource node will always have unique value even if multiple component is added on the same page and work correctly.
${resource.path.hashCode} => will have unique value for each component
${rowList.count} => will be unique for each row withing that corresponding component
${columnList.count} => will be unique for each column withing that corresponding component
 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/using-data-sly-list-with-m...

 

Thanks

Avatar

Community Advisor

@SudarshanV1 xyz should be a variable like xyz1, xyz2, etc. in place of constant for all.

Avatar

Community Advisor

Hi @SudarshanV1 ,

Avatar

Community Advisor

@SudarshanV1 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!


Aanchal Sikka