Expand my Community achievements bar.

SOLVED

Nested Ternary expression

Avatar

Level 4

Hi all,

I am trying to implement something like below in sightly

<div class="${condition1?'class1:'condition2?'class2':'condition3?'class3':''''}">

I have 3 conditions, if condition1 is not met it goes to condition2 and then condition 3 and finally else case. I am getting error doing above.

Below is general logic

if(con1)

else if(con2)

else if(con3)

else

Please let me know how to implement in correct way in sightly.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Here is the example

<sly data-sly-test.if="${pageProperties.jcr:title == 'English1'}"></sly>

<sly data-sly-test.elseif1="${pageProperties.jcr:title == 'English2'}"></sly>

<sly data-sly-test.elseif2="${pageProperties.jcr:title == 'English'}"></sly>

<div class="${if?'class1':(elseif1?'class2':(elseif2?'class3':''))}">Demo</div>

In your case

<div class="${condition1?'class1':(condition2?'class2':(condition3?'class3':''))}">Demo</div>



Arun Patidar

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Here is the example

<sly data-sly-test.if="${pageProperties.jcr:title == 'English1'}"></sly>

<sly data-sly-test.elseif1="${pageProperties.jcr:title == 'English2'}"></sly>

<sly data-sly-test.elseif2="${pageProperties.jcr:title == 'English'}"></sly>

<div class="${if?'class1':(elseif1?'class2':(elseif2?'class3':''))}">Demo</div>

In your case

<div class="${condition1?'class1':(condition2?'class2':(condition3?'class3':''))}">Demo</div>



Arun Patidar