Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
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



Kautuk Sahni

Avatar

Correct answer by
Level 9

Hi All,

Thanks a lot for your reply.

Tried the below :

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

Avatar

Level 1

Could you please elaborate on your solution? Bit Urgent