Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

ternary operator inside link tag not working in sightly

Avatar

Level 2

Hi All,

I have to write a condition inside sightly, where I wanted something like this.

 

Anoop345_0-1650357140201.png

if country coming from list is international then I have to show only locale in hreflang else locale-country.

I tried above ternary operator but its not working. any suggestion.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Per the Block statements priority, data-sly-set/data-sly-test is evaluated before data-sly-repeat.

ie. analyticsItem identifier is not available when data-sly-set/test executes and only "-" is set to the concatenated identifier and hence to the hreflang attribute. 

https://experienceleague.adobe.com/docs/experience-manager-htl/using/htl/block-statements.html#avail...

You can move the String concatenation to backend and have it accessible as straightforward/single value (like you have locale/country)

View solution in original post

10 Replies

Avatar

Community Advisor

Hi @Anoop345 ,

 

The whole operation within ternary operator needs to be inside the curly braces {}.

So , changing your code to : 

 

hreflang= ${analyticsItem.country == 'international'  ? analyticsItem.locale : analyticsItem.locale-analyticsItem.country}

should work.

 

Avatar

Level 2

Hi @Anmol_Bhardwaj ,

Thanks for your reply.

I tried above code but its not working. 

Avatar

Community Advisor

Hi @Anoop345,

In my opinion you should do this operation inside your SlingModel level not in HTL.

Nevertheless it should be possible to achieve what you want in HTL using below code. In general you will need to add additional line to create concatenated string before it is used:

<sly data-sly-test.concatenated="${'{0}-{1}' @ format=[analyticsItem.locale, analyticsItem.country]}"/>
hreflang= ${analyticsItem.country == 'international' ? analyticsItem.locale : concatenated}

 

Avatar

Level 2

Thanks for reply.

I tried it but seems like issue is with <link> tag, as its giving error while writing the condition inside it.

<link
data-sly-repeat.analyticsItem="${head.AnalyticsItems}"
data-sly-test.concatenated="${'{0}-{1}' @ format=[analyticsItem.locale, analyticsItem.country]}"
hreflang= ${analyticsItem.country == 'international' ? analyticsItem.locale : concatenated}
rel="alternate"
href="${analyticsItem.path}"
/>

Avatar

Community Advisor

Hi,

I think, the code suggested by @lukasz-m should work but can your try with below condition

data-sly-test.concatenated="${[analyticsItem.locale, analyticsItem.country] @ join = '-'}"

 



Arun Patidar

Avatar

Community Advisor

@Anoop345 

hreflang attribute value is not enclosed in double quotes.

Also, you can use data-sly-set for framing concatenated String instead of data-sly-test

 

Use this markup (Amended on top of your latest)

<link
data-sly-repeat.analyticsItem="${head.AnalyticsItems}"
data-sly-set.concatenated="${'{0}-{1}' @ format=[analyticsItem.locale, analyticsItem.country]}"
hreflang="${analyticsItem.country == 'international' ? analyticsItem.locale : concatenated}"
rel="alternate"
href="${analyticsItem.path}"
/>

Avatar

Community Advisor

Please check the HTL version and it's compatibility for data-sly-set use

https://github.com/adobe/htl-spec/releases

I think it is compatible with AEM6.4 SP2 and later.



Arun Patidar

Avatar

Level 2

Thanks for the reply.

I tried both data-sly-test and data-sly-set but not sure why the value of "concatenated" is coming only "-" in the hreflang, however analyticsItem.country and analyticsItem.locale coming properly from the model. So if country is not international its coming like this in the output-

<link rel="alternate" hreflang="-" href="http://localhost:4502/content/myproject/content/br/en/test.html"/>

Avatar

Correct answer by
Community Advisor

Per the Block statements priority, data-sly-set/data-sly-test is evaluated before data-sly-repeat.

ie. analyticsItem identifier is not available when data-sly-set/test executes and only "-" is set to the concatenated identifier and hence to the hreflang attribute. 

https://experienceleague.adobe.com/docs/experience-manager-htl/using/htl/block-statements.html#avail...

You can move the String concatenation to backend and have it accessible as straightforward/single value (like you have locale/country)

Avatar

Level 2

Thanks @Vijayalakshmi_S ,

Yes, its because of block statements priority. I fixed this issue from backend:)