rohan
rohan

Reputation: 33

Sightly expression to compare string property with list count

I am trying to compare two values, one coming from dialog and other is count var of Map. But getting an error:

Caused by: java.lang.UnsupportedOperationException: Invalid types in comparison. Comparison is supported for Number types only

This is simple code of getting the List of links of pages, but if max count set will show restricted(max) no. of links. If three set as max count only 3 links will come in list.

<div id="${properties.containerId}" class="${properties.containerClass}">
<div data-sly-use.listOfLink="com.aem.web.core.components.ListOfLink" data-sly-unwrap>

    <div data-sly-list.keyName="${listOfLink.pageMap}" data-sly-unwrap>
        <div data-sly-test.maxcount="${properties.maxcount}" data-sly-unwrap>
        <!-- Check the max count set in dialog(property ) for link to display from Map-->
            <div data-sly-test="${keyName.count <= properties.maxcount}"  data-sly-unwrap>
                <p><a href="${keyName}">${listOfLink.pageMap[keyName][0]</a></p>
            </div>

        </div>

    </div>
</div>

Upvotes: 1

Views: 9188

Answers (3)

Gabriel Walt
Gabriel Walt

Reputation: 1629

For following comparison operators: < <= >= >, Sightly requires numeric types. But it doesn't matter where the variables come from, and it works as expected whether they come from a property or from the list counter (or a mix of both). It is likely that in the content repository, your value has been stored as text (String) and not as a number (Decimal, Double, or Long). Either tweak your dialog to use a Number Field, in order to store the value as a number in the repository, or, as andeh suggested, make the conversion in the Use-API.

Then, as Christopher noted, the object holding the count variable of your list ist itemList, or as your renamed the item variable, it is keyNameList in your case.

Additionally, note that the goal of Sightly is to reuse the HTML elements for the template logic, in order to not clutter the markup with template blocks. But adding each Sightly instruction on a separate <div> element, and then removing them again with data-sly-unwrap really should be avoided! It makes the markup very hard to read, as one doesn't know anymore which <div> will get displayed and which one is only for logic.

So your template could be written as follows, without any data-sly-unwrap:

<div id="${properties.containerId}"
     class="${properties.containerClass}"
     data-sly-use.listOfLink="com.aem.web.core.components.ListOfLink"
     data-sly-list.keyName="${listOfLink.pageMap}">
    <p data-sly-test="${properties.maxcount && keyNameList.count <= properties.maxcount}">
        <a href="${keyName}">${listOfLink.pageMap[keyName][0]}</a>
    </p>
</div>

And if you cannot change the dialog to store numbers, in the Use-API you can force your string property to be a number. The following code illustrates that the ListOfLink object extends WCMUsePojo:

public int getMaxcount() {
    return getProperties().get("maxcount", 0);
}

Which you can then access in your template as ${listOfLink.maxcount}.

Also useful might be the following Sightly Style Guide from Netcentric.

Upvotes: 1

Cris Rockwell
Cris Rockwell

Reputation: 904

It seems the object holding the variables is the list and not the listed item. In the link it shows an example...

http://docs.adobe.com/docs/br/aem/6-0/develop/sightly.html#list

<div data-sly-test="${keyNameList.count <= properties.maxcount}" data-sly-unwrap>

Upvotes: 0

ydnaklementine
ydnaklementine

Reputation: 330

Sling inject maxcount into your java class. Create a getter, and retrieve it in sightly with listOfLink.maxcount.

<div data-sly-test="${keyName.count <= listOfLink.maxcount}"  data-sly-unwrap>
            <p><a href="${keyName}">${listOfLink.pageMap[keyName][0]</a></p>
</div>

Upvotes: 0

Related Questions