How to handle else/if condition in sightly : Bit Urgent | Community
Skip to main content
Level 9
June 8, 2016
Solved

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

  • June 8, 2016
  • 5 replies
  • 41952 views

[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.
Best answer by askdctm

Hi All,

Thanks a lot for your reply.

Tried the below :

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

5 replies

edubey
Level 10
June 8, 2016

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

askdctmAuthor
Level 9
June 8, 2016

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.

Level 4
June 8, 2016

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>
kautuk_sahni
Community Manager
Community Manager
June 9, 2016

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

Kautuk Sahni
askdctmAuthorAccepted solution
Level 9
June 9, 2016

Hi All,

Thanks a lot for your reply.

Tried the below :

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

August 11, 2022

Could you please elaborate on your solution? Bit Urgent