htmllistaemsightlyhtl

Sightly: Iterating over a list and using its index to print another list (not nested, just sharing the index)


I have 2 lists in sightly. list1 and pathList inside object allList

<div data-sly-list.list1="${allList.list1}">
    <img src="soemthing">
    <a href="XXXXXXXXX">${list1}</a>
</div>

At the place where I had written "XXXXXXXXX", I need to place soemthing like

${pathList['list1List.index']}

OR

${allList.pathList['list1List.index']}

So, basically I want to iterate the second list using the index of the first, and I cannot use nested loop.

But whenever I am trying to use the syntax I mentioned above, I get template parse error because the syntax is wrong. Somebody please guide me as to how to achieve this.


Solution

  • You have to use the index without the ' ', like this: ${allList.pathList[list1List.index]}

    Example:

    @Model(adaptables = Resource.class)
    public class TestModel {
    
        @Inject
        Resource resource;
    
        private String[] abc = {"a", "b", "c"};
        private String[] def = {"d", "e", "f"};
        ...
    }
    

    HTL:

    <sly data-sly-list.first="${model.abc}">
        <p>Test: ${first}  -  ${model.def[firstList.index]}</p>
    </sly>
    

    Output HTML:

    <p>Test: a  -  d</p>
    <p>Test: b  -  e</p>
    <p>Test: c  -  f</p>