Skip to content

HTL data-sly-list: Iterating Over Lists and Maps

data-sly-list in HTL: syntax, itemList index/count/first/last/odd/even, named loops, maps, empty-list handling, and the HTL 1.4 begin, end and step options.

Rewritten in November 2026 and checked against the HTL specification, Adobe's HTL documentation and the source of the Apache Sling HTL engine that AEM runs.

data-sly-list is HTL's loop. It writes the host element once and renders everything inside it again for each item of a collection. Inside the loop the current value is called item, and a second variable, itemList, says where you are: index, count, first, last. When the collection is empty, the host element and its content are left out of the output.

<ul class="cmp-subnav" data-sly-list="${currentPage.listChildren}">
 <li><a href="${item.path}.html">${item.title}</a></li>
</ul>

A page with three children gets one ul with three li elements. A page with none gets nothing, so you don't need a separate test to avoid printing an empty list.

What can data-sly-list iterate over?

Anything the engine can turn into a collection. The Sling engine accepts a Collection, Iterator, Iterable, Enumeration or array. A Map yields its keys, and a lone String or Number becomes a one-item list. That last rule is why a loop handed a string, where you meant an array, runs exactly once.

How do I rename item? (data-sly-list.child)

Put an identifier after a dot. The item variable takes that name, and the status variable becomes the same name with List appended:

<ol data-sly-list.child="${currentPage.listChildren}">
 <li>${childList.count}. ${child.title}</li>
</ol>

Name every loop as soon as you nest two. With two anonymous loops the inner item hides the outer one, and you can't reach the parent page from inside. The identifier exists only inside the host element, and a variable it shadowed gets its old value back once the loop ends.

<ul class="cmp-sitemap" data-sly-list.section="${currentPage.listChildren}">
 <li>${section.title}
 <ul data-sly-list.subPage="${section.listChildren}">
 <li><a href="${subPage.path}.html">${subPage.title}</a> (in ${section.title})</li>
 </ul>
 </li>
</ul>

itemList properties: index, count, first, middle, last, odd, even

MemberValue
indexPosition from 0 to length − 1
countPosition from 1 to length
firsttrue on the first item
middletrue on every item that is neither first nor last
lasttrue on the last item
oddtrue when count is odd: the 1st, 3rd, 5th item
eventrue when count is even

HTL 1.4 defines odd and even by count, so the first item is odd. The 1.3 text of the specification said "index", which gives the opposite answer. The Sling engine follows 1.4.

<table class="cmp-pricelist">
 <tbody data-sly-list.row="${model.rows}">
 <tr class="${rowList.odd ? 'cmp-pricelist__row--shaded' : 'cmp-pricelist__row'}">
 <td>${row.name}</td>
 <td>${row.price}</td>
 </tr>
 </tbody>
</table>

<p class="cmp-tags" data-sly-list.tag="${model.tagTitles}">${tag}<sly data-sly-test="${!tagList.last}">, </sly></p>

The second loop prints a comma after every tag but the last. For a plain string list, the join option from HTL expression options does the same in one expression; the loop earns its place once each tag needs its own link or markup.

How do I loop over a Map?

The loop variable holds the key. Look up the value with bracket access on the map itself:

<dl class="cmp-specs" data-sly-list.key="${model.specs}">
 <dt>${key}</dt>
 <dd>${model.specs[key]}</dd>
</dl>

Keys come out in the map's own iteration order. A HashMap returned by a Sling Model has none worth relying on, so return a LinkedHashMap or a TreeMap when the order shows on the page.

What happens when the list is empty?

There's no else for loops. Because the host element disappears with an empty collection, the "nothing here" message goes on a sibling element guarded by a test of the same value. Store the value once so the model isn't asked twice:

<sly data-sly-set.results="${model.results}"/>
<ul class="cmp-search__results" data-sly-list.result="${results}">
 <li><a href="${result.url}">${result.title}</a></li>
</ul>
<p class="cmp-search__empty" data-sly-test="${!results}">No articles match this filter yet.</p>

Have getResults() return a List. An Iterator can be walked once: the loop exhausts it, and the test after it then sees an empty iterator and shows the message under a full list. The data-sly-test page has the full truth table.

begin, end and step: rendering part of a list

Three options picking a slice of the collection arrived with HTL 1.4 in June 2018, and they apply to data-sly-repeat as well. begin is the first index rendered, end the last one (inclusive), and step the stride.

<!--/* the first five children: indexes 0 to 4 */-->
<ul data-sly-list.child="${currentPage.listChildren @ end=4}">
 <li>${child.title}</li>
</ul>

<!--/* every second child, starting with the second: indexes 1, 3, 5 ... */-->
<ul data-sly-list.child="${currentPage.listChildren @ begin=1, step=2}">
 <li>${child.title}</li>
</ul>

If begin is equal to or larger than the collection's size, the host element isn't rendered. Two more behaviours aren't in the specification but are plain in the engine's list code. The status variable keeps counting across the whole collection, so with begin=10 the first rendered item has index 10, count 11, and first is false. And the loop still visits every item, skipping the ones outside the window; for a long result set, trim it in the Sling Model.

data-sly-list or data-sly-repeat?

Choose by what should repeat. data-sly-list writes the host once and repeats its content, which suits a container such as ul, tbody or dl. data-sly-repeat repeats the host element itself, which suits a run of sibling cards with no wrapper:

<div class="cmp-card" data-sly-repeat.card="${model.cards}">${card.title}</div>

Both expose the same status members and the same three options.

Why can't data-sly-test see item on the same element?

Block statements on one element run in a fixed order, and data-sly-test runs before data-sly-list. A test on the loop's host is evaluated once, before the first item exists. Filter inside the loop on a child element, or on <sly> when no child fits:

<ul data-sly-list.child="${currentPage.listChildren}">
 <sly data-sly-test="${!child.properties.hideInNav}">
 <li><a href="${child.path}.html">${child.title}</a></li>
 </sly>
</ul>

Skipped items still count, so childList.last may point at a hidden page. When the counters matter, filter in Java first. The full priority table is in the HTL overview.