How to handle else/if condition in sightly : Bit Urgent | Community
Skip to main content
Level 9
Jun 8, 2016 3:56 PM UTC
Solved

How to handle else/if condition in sightly : Bit Urgent

[Thread Edited By Adobe]

/*Don’t forget to meet and greet your fellow peers virtually by telling them about yourself here. 

Go ahead and to it now: https://adobe.ly/3eDnB4v */

 

Actual Post:

Hi All,

In my sightly code I have two page properties which I am successfully getting from my Java WCMUse class.

${bean.getProperty1}  and ${bean.getProperty2}.

I am able to print these values.

#1] However, inside <p></p> tag, I need to have a logic such that it should check if ${bean.getProperty1} is available. If yes, print that , else print the second property.

I am trying the below :

#2] <p class="article-label">"${bean.getProperty1}?${bean.getProperty1}:${bean.getProperty2}"</p>

But its not coming up properly on the page. By that I mean say value1 and value2 are corresponding properties and if property1 is present its printing "value1?value1:" on the page, rather than just a single value.

Any thoughts/pointers on this will be very helpful.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
askdctmAccepted solution
Level 9
Jun 9, 2016 5:23 PM UTC

Hi All,

Thanks a lot for your reply.

Tried the below :

"${property1 || property2}" and it worked.

6 replies

askdctmAuthorAccepted solution
Level 9
Jun 9, 2016 5:23 PM UTC

Hi All,

Thanks a lot for your reply.

Tried the below :

"${property1 || property2}" and it worked.

Translate
New Participant
Aug 11, 2022 5:22 PM UTC

Could you please elaborate on your solution? Bit Urgent

Translate
kautuk_sahni
Employee
Jun 9, 2016 10:14 AM UTC

askdctm wrote...

Hi Praveen,

Thank you for your reply.

Somehow, I am not quite getting as to why its giving output as  "value1?value1:" .

If you can provide a snippet, it would be helpful.

 

Hi

Please have a look at this post:-

Link:- http://blogs.adobe.com/experiencedelivers/experience-management/sightly-intro-part-1/

//

<ul data-sly-list.child="${currentPage.listChildren}">
  <li class="${ childList.odd ? 'odd' : 'even'}">${child.title}</li>
</ul>

Link:- http://stackoverflow.com/questions/24550498/conditional-attributes-in-sightly-templates-aem-cq

//

Ternary conditional operator

<ul data-sly-list="${items}" class="${condition1 ? 'selected' : ''}">
    <li class="${condition2 ? 'selected' : ''}">
        Lots of other markup here
    </li>
</ul>

I hope this will help you.

Thanks and Regards

Kautuk Sahni

Translate
Kautuk Sahni
Level 4
Jun 8, 2016 4:20 PM UTC

You're printing out the string literals for all the values, without processing them. The three expressions in the ternary operator are being evaluated in isolation from one another. You should move the statements within a singel expression tag (${}).

<p class="article-label">${'{0}' @format=[bean.getProperty1?bean.getProperty1:bean.getProperty2]}</p>
Translate
askdctmAuthor
Level 9
Jun 8, 2016 4:13 PM UTC

Hi Praveen,

Thank you for your reply.

Somehow, I am not quite getting as to why its giving output as  "value1?value1:" .

If you can provide a snippet, it would be helpful.

Translate
edubey
Level 10
Jun 8, 2016 4:05 PM UTC

Use data-sly-test to check for existence, which u can also store in a variable as result. 

Read docs for this: https://github.com/Adobe-Marketing-Cloud/sightly-spec/blob/master/SPECIFICATION.md#225-test

Translate