How to handle if condition in sightly
If ${item.condition1} satisfies, then I want "testclass" to be added in div
Ex:<div class="exampleclass testclass">
Else no testclass should be added in div
Ex: <div class="exampleclass>
Thanks in advance
If ${item.condition1} satisfies, then I want "testclass" to be added in div
Ex:<div class="exampleclass testclass">
Else no testclass should be added in div
Ex: <div class="exampleclass>
Thanks in advance
Using the sightly features, you can use the data-sly-test.
A standard practice is to use ternary conditions in Sightly HTL. While using Sightly, I would like to highlight that you can use Slightly Global objects for rapid development. AEM Global Objects for Backend and Front-end Sightly (HTL) Development - Sourced Code; make sure you read #2, so you understand how Global AEM objects can be used.
Note: if your conditions are more complicated and require some kind of business logic, such as calculating a collection of HTML classNames, you can always place the write logic into a Sling Model, Java-Use-API, JavaScript-Use-API, and have the backend return the CSS classNames as you please.
Here are some code examples that you can paste into your Sightly HTL to test the code:
scenario 1: true
//sightly
<div class="exampleclass ${true ? 'testclass' : ''}">
//output:
<div class="exampleclass testclass ">
scenario 2: false
//sightly
<div class="exampleclass ${false ? 'testclass' : ''}">
//output:
<div class="exampleclass ">
scenario 3: evaluate to true
//sightly
<div class="exampleclass ${currentPage.path === '/content/my-site/home' ? 'testclass' : ''}">
//output:
<div class="exampleclass testclass">
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.