ternary operator inside link tag not working in sightly | Community
Skip to main content
Level 2
April 19, 2022
Solved

ternary operator inside link tag not working in sightly

  • April 19, 2022
  • 2 replies
  • 5946 views

Hi All,

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

 

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.

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 Vijayalakshmi_S

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"/>

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#available-block-statements

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

2 replies

Anmol_Bhardwaj
Community Advisor
Community Advisor
April 19, 2022

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.

 

Anoop345Author
Level 2
April 19, 2022

Hi @anmol_bhardwaj ,

Thanks for your reply.

I tried above code but its not working. 

lukasz-m
Community Advisor
Community Advisor
April 19, 2022

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}

 

Anoop345Author
Level 2
April 19, 2022

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}" />
Anoop345Author
Level 2
April 19, 2022

@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}"
/>

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"/>