如何在模板中写条件?

时间:2017-09-18 02:55:26

标签: javascript adobe aem sightly htl

是否根据条件进行模板渲染?

e.g。 component.html基于参数渲染

如果存在参数

<div data-sly-use.myComponent="${'com.myproject.service' @ param1='one''}">
    ${myComponent.calculatedValue}
</div>

如果参数2存在

<div data-sly-use.myComponent="${'com.myproject.service' @ param2='one''}"">
    ${myComponent.calculatedValue}
</div>

如果参数不存在

<div data-sly-use.myComponent="${'com.myproject.MyComponent'}">
    ${myComponent.calculatedValue}
</div>

问题2:如何获得param1 ='one'值(来自javascript或jsp)

问题3:是否可以对此值进行字符串操作$ {myComponent.calculatedValue}

1 个答案:

答案 0 :(得分:0)

使用data-sly-test(参见SPEC)可以在HTL / Sightly中进行条件渲染:

<div data-sly-test="${param1 == 'one'}"..>..</div>
<div data-sly-test="${param2 == 'one'}"..>..</div>
<div data-sly-test="${param1 != 'one' && param2 != 'one'}"..>..</div>

这允许您基于参数实例化不同的使用对象。该参数必须内联定义,通过可用的绑定/ global objects或另一个使用对象访问。

此时HTL表达式中的操作为not supported