How to write condition in sightly template?

2.7k views Asked by At

Does sightly template render based on a condition?

e.g. 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}

1

There are 1 answers

1
Vlad On

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.