HTL Ternary operation with selectors and extensions | Community
Skip to main content
Level 3
October 28, 2020
Solved

HTL Ternary operation with selectors and extensions

  • October 28, 2020
  • 3 replies
  • 1908 views

I'm trying to write a HTL statement using ternary operation with selectors and extension. But html is not compiling and throwing error.

${slingModel.shouldHaveSelector ? slingModel.someLink @ selectors='content', extension = 'html' : slingModel.someLink @ extension = 'html' }

Checked in HTL docs, but there is no mention of using ternary operations with extension and selectors.

Any help on the right syntax to be used? 

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 Suraj_Kamdi

@ashwin_raju 

correct way to write the expression is as follow

${slingModel.shouldHaveSelector ? slingModel.someLink  : slingModel.someLink @ selectors='content', extension = 'html' }

 

but in your case please rewrite the logic as

<sly data-sly-test={slingModel.shouldHaveSelector}> {slingModel.somelink @ selectors='content', extension = 'html'}</sly> 

<sly data-sly-test={!slingModel.shouldHaveSelector}> {slingModel.somelink @ extension = 'html'}</sly> 

3 replies

Suraj_Kamdi
Community Advisor
Suraj_KamdiCommunity AdvisorAccepted solution
Community Advisor
October 28, 2020

@ashwin_raju 

correct way to write the expression is as follow

${slingModel.shouldHaveSelector ? slingModel.someLink  : slingModel.someLink @ selectors='content', extension = 'html' }

 

but in your case please rewrite the logic as

<sly data-sly-test={slingModel.shouldHaveSelector}> {slingModel.somelink @ selectors='content', extension = 'html'}</sly> 

<sly data-sly-test={!slingModel.shouldHaveSelector}> {slingModel.somelink @ extension = 'html'}</sly> 

Level 3
October 28, 2020
Thanks Suraj!
Anudeep_Garnepudi
Community Advisor
Community Advisor
October 28, 2020

Hi @ashwin_raju 

Use the below modified expression with one line. Your expression is wrong. Inside expression, upon a condition you can append or add String but can't send parameters(use @).

${slingModel.someLink @ extension = 'html', selectors= slingModel.shouldHaveSelector ? 'content' : ' ' }

Note: Added space in between single quotes of else to differentiate with double quote.

AG 

AG
Manjunath_K
Level 7
October 28, 2020

Hi @ashwin_raju 

We can use ternary operation as mentioned below.

 

${slingModel.someLink @ selectors = slingModel.shouldHaveSelector ? 'content' : '', extension = 'html'}

 

 

Hope this helps!