Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

HTL Ternary operation with selectors and extensions

Ashwin_Raju
Level 3
Level 3

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? 

1 Accepted Solution
Suraj_Kamdi
Correct answer by
Community Advisor
Community Advisor

@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> 

View solution in original post

4 Replies
Suraj_Kamdi
Correct answer by
Community Advisor
Community Advisor

@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> 

View solution in original post

Anudeep_Garnepudi
Community Advisor
Community Advisor

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 

Manjunath_K
Community Advisor
Community Advisor

Hi @Ashwin_Raju 

We can use ternary operation as mentioned below.

 

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

 

 

Hope this helps!