Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

Avatar

Level 9

[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.

desktop_exl_promo_600x100_weempoweryou.png

1 Accepted Solution

Avatar

Correct answer by
Level 9

Hi All,

Thanks a lot for your reply.

Tried the below :

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

View solution in original post

6 Replies

Avatar

Level 10

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

Avatar

Level 9

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.

Avatar

Level 5

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>

Avatar

Administrator

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

Avatar

Correct answer by
Level 9

Hi All,

Thanks a lot for your reply.

Tried the below :

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