简体   繁体   中英

How to write condition in sightly template?

Does sightly template render based on a condition?

eg component.html render based on arguments

if argument one exist

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

if argument two exist

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

if argument doesn't exist

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

Question 2: How to get param1='one' value (from javascript or jsp)

Question 3: is it possible to do string operation on this value ${myComponent.calculatedValue}

Conditional rendering in HTL/Sightly is possible by using data-sly-test (see SPEC ):

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

This allows you to instantiate different Use Objects based on a parameter. The parameter must be defined inline, accessed via the available bindings/ global objects or via another Use Object.

Operations are not supported in HTL expressions at this moment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM