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.
Solved! Go to Solution.
Views
Replies
Total Likes
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.
You can move the String concatenation to backend and have it accessible as straightforward/single value (like you have locale/country)
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.
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}
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}"
/>
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 = '-'}"
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)
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.
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.
You can move the String concatenation to backend and have it accessible as straightforward/single value (like you have locale/country)
Thanks @Vijayalakshmi_S ,
Yes, its because of block statements priority. I fixed this issue from backend:)
Views
Like
Replies
Views
Likes
Replies
Views
Likes
Replies