HTL Ternary operation with selectors and extensions

Avatar

Avatar

Ashwin_Raju

Avatar

Ashwin_Raju

Ashwin_Raju

28-10-2020

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? 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Suraj_Kamdi

MVP

Avatar

Suraj_Kamdi

MVP

Suraj_Kamdi
MVP

28-10-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> 

Answers (2)

Answers (2)

Avatar

Avatar

Manjunath_K

Avatar

Manjunath_K

Manjunath_K

28-10-2020

Hi @Ashwin_Raju 

We can use ternary operation as mentioned below.

 

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

 

 

Hope this helps!

Avatar

Avatar

Anudeep_Garnepudi

Avatar

Anudeep_Garnepudi

Anudeep_Garnepudi

28-10-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