将字符串 属性 与列表计数进行比较的直观表达式

Sightly expression to compare string property with list count

我正在尝试比较两个值,一个来自对话框,另一个是地图的计数变量。但是出现错误:

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

这是获取页面链接列表的简单代码,但如果设置了最大计数,将显示限制(最大)否。的链接。如果三个设置为最大计数,列表中将只有 3 个链接。

<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>

Sling 将 maxcount 注入您的 java class。创建一个 getter,然后使用 listOfLink.maxcount.

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

保存变量的对象似乎是列表而不是列出的项目。在 link 中显示了一个示例...

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

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

对于以下比较运算符:< <= >= >,Sightly 需要数字类型。但变量来自何处并不重要,无论它们来自 属性 还是来自列表计数器(或两者的混合),它都会按预期工作。很可能在内容存储库中,您的值已存储为文本 (String) 而不是数字(Decimal、Double 或 Long)。调整您的对话框以使用数字字段,以便将值作为数字存储在存储库中,或者按照安德的建议,在 Use-API.

中进行转换

然后,正如 Christopher 指出的那样,保存列表 itemListcount 变量的对象,或者如您重命名的 item 变量,它是 keyNameList在你的情况下。

此外,请注意 Sightly 的目标是 重用 模板逻辑的 HTML 元素,以免标记与模板块混淆。但是在单独的 <div> 元素上添加每个 Sightly 指令,然后用 data-sly-unwrap 再次删除它们确实应该避免!它使标记很难阅读,因为人们不再知道将显示哪个 <div> 以及哪个仅用于逻辑。

所以你的模板可以这样写,没有任何 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>

如果你不能改变对话框来存储数字,在 Use-API 你可以强制你的字符串 属性 是一个数字。以下代码说明了 ListOfLink 对象扩展了 WCMUsePojo:

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

然后您可以在模板中访问 ${listOfLink.maxcount}

以下内容可能也很有用 Sightly Style Guide from Netcentric