I am trying to execute this snippet by using conditional operator but fails- sightly | Community
Skip to main content
arjunsudhas1995
Level 2
February 1, 2022
Solved

I am trying to execute this snippet by using conditional operator but fails- sightly

  • February 1, 2022
  • 2 replies
  • 1341 views

I am trying to execute the following code snippet by using the conditional operator in sightly/htl:

 

<a href="${author ? footer.root.path @ extension = 'html' : footer.root.path}"
                                class="-oneX-link--block text-gray-100">${footer.root.title}</a>
 

but it is failing with the following error:

org.apache.sling.api.scripting.ScriptEvaluationException: ${author ? footer.root.path @ extension = 'html' : footer.root.path}: mismatched input '@' expecting {'.', 'in', '&&', '||', '[', ':'} in /apps/StateFarmAEM/components/product-footer-nav/product-footer-nav.html at line number 10 at column number 104

How to correct this expression

Please help

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

@arjunsudhas1995 

As evident in the error description, Expression option(@) is not allowed within Ternary operator expression (instead as a whole upon conditional evaluation)

Example : Below is allowed in which case, extension is applied to both the scenarios. (will result in abc.html or xyz.html)

${request.parameterMap.firstName ? 'abc' : 'xyz' @ extension='html'}

For your use case, I would suggest to move the conditional logic to Sling Model and just use the footer.root.path 

(Given that need for condition is itself whether or not to apply extension based on condition)

2 replies

Kishore_Kumar_
Level 9
February 1, 2022

Hi @arjunsudhas1995 ,

 

 Since ternary operator expects any of these symbols 

 

{'.', 'in', '&&', '||', '[', ':'}

 

Try using the extension option in else part.

 

<a href="${!author ? footer.root.path : footer.root.path @ extension = 'html'}"
                                class="-oneX-link--block text-gray-100">${footer.root.title}</a>
Vijayalakshmi_S
Level 10
February 1, 2022

@kishore_kumar_ 

This would apply the extension in either case. 

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
February 1, 2022

@arjunsudhas1995 

As evident in the error description, Expression option(@) is not allowed within Ternary operator expression (instead as a whole upon conditional evaluation)

Example : Below is allowed in which case, extension is applied to both the scenarios. (will result in abc.html or xyz.html)

${request.parameterMap.firstName ? 'abc' : 'xyz' @ extension='html'}

For your use case, I would suggest to move the conditional logic to Sling Model and just use the footer.root.path 

(Given that need for condition is itself whether or not to apply extension based on condition)

arjunsudhas1995
Level 2
February 1, 2022

according to my usecase I only want extension to be applied when condition is true

Vijayalakshmi_S
Level 10
February 1, 2022

Handle the conditional logic in Sling Model.