使用AEM的Javascript Use-API,如何将函数传递给HTL端并使用参数调用它?



在Adobe AEM中,假设我有一个JavascriptUse-API文件,该文件将传递回以下数据:

文件:secure.js

use( function() {
var pages = ['home', 'about', 'contact'];
return {
pages: pages,
isSecurePage: function(pageName) {
return pages.indexOf(pageName) > -1;
}
});

那么在HTL中,如何调用isSecurePage方法并向其传递所需的参数?

我试过这个:

文件:home.html

<sly data-sly-use.secure="./secure.js" />
<div class="row" data-sly-list.child="${currentPage.listChildren}">
<sly data-sly-test="${secure.isSecurePage(child.getName)}"> <!-- ERROR! -->
<a href="${child.getPath}.html">${child.getName}</a>
</sly>
</div>

但我得到了一个这样的错误:

Parsing error in template ...

... extraneous input '(' expecting {'}', '.', '&&', '||', '[', '@'} for expression ${secure.isSecurePage( child.getName ) }


我试图以不同的方式重写它们,看看是否可以调用该方法,但这里的一切都失败了:

1(secure.isSecurePage @ child.getName

2(secure.isSecurePage @ 'child.getName' )

3(secure.isSecurePage @ 0=child.getName )


虽然下面的这些不会导致错误,但它似乎没有正确地传递参数:

4(secure.isSecurePage @ first=child.getName )

5(secure.isSecurePage @ pageName=child.getName )

^保持返回false,即使它应该是true

我将尝试找出一种从Javascript Use-API登录的方法来进一步研究这一点。

如果有人知道,请帮忙!

感谢

不能调用在HTL中接受参数的方法。唯一允许参数的构造是模板和Use对象实例化。有关更多详细信息,请查看规范的data-sly-use[0]和Use-API[1]部分。

[0]-https://github.com/adobe/htl-spec/blob/1.4/SPECIFICATION.md#221-使用
[1]-https://github.com/adobe/htl-spec/blob/1.4/SPECIFICATION.md#4-使用api

最新更新