如何在 sightly 中使用条件语句时追加字符?

How to append a character while using the conditional statement in sightly?

我需要根据 sling:vanityPath 的无效性设置 href 值。 hit.properties.sling:vanityPath returns 的字符串值为sling:vanityPath 属性。我需要在 hit.properties.sling:vanityPath 值之前附加一个 /。是否可以在以下代码中执行此操作,或者我是否必须对其进行两次测试,但代码重复?

<a href="${hit.properties.sling:vanityPath == null? hit.path : hit.properties.sling:vanityPath @ extension='html'}"/>

HTL 不支持串联或二元运算符。您可以使用 prependPath 或双重测试斜杠。

我不认为你可以一行完成。但是您可以尝试为连接值定义一个变量,而不是为 a 标签复制代码。另外,您是否必须专门检查 null ?就不能把条件倒过来检查值是否存在吗?

<sly data-sly-test.concatenatedURL="${['/', hit.properties.sling:vanityPath] @ join = ''}"/>
<a href="${hit.properties.sling:vanityPath ? concatenatedURL : hit.path @ extension='html'}"/>

您可以使用@join 来连接一个数组:

<a href="${hit.properties.sling:vanityPath ? ['/', hit.properties.sling:vanityPath] : hit.path @ join ='', 'extension='html'}"/>