Search code examples
aemsightly

How to access a string array using Sightly(HTL)


How can i access a string array coming from a model class using sightly(HTL)

The TestModel is a model class that returns a string array , getResult() is the getter used to return the string array

how can I use sightly to get it??

    <p>display output :</p>
    <sly data-sly-use.object = "com.silversea.core.models.TestModel">
      <sly data-sly-list.mylist = "${object.Result}">      //what command show we use instead of data-sly-list
        <p>1st text: ${item} </p>
      </sly>
    </sly>

Solution

  • The problem you are facing here is caused by two things:

    Defining an identifier on the data-sly-list statement allows you to rename the itemList and item variables. item will become variable and itemList will become variableList

    More details in https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/block-statements.html

    So in your example you must change ${item} into ${mylist}

    <p>display output :</p>
    <sly data-sly-use.object = "com.silversea.core.models.TestModel">
      <sly data-sly-list.mylist = "${object.result}">      //what command show we use instead of data-sly-list
        <p>1st text: ${mylist} </p>
      </sly>
    </sly>
    

    The second thing is that you should also follow the java bean naming convention: So if you have a getter getResult() then in HTL you should use ${object.result} (starting from lowercase)