Expand my Community achievements bar.

Nomination window for the Adobe Community Advisor Program, Class of 2025, is now open!
SOLVED

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

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Level 10

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

View solution in original post

5 Replies

Avatar

Level 10

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>

Avatar

Level 10

@Kishore_Kumar_ 

This would apply the extension in either case. 

Avatar

Correct answer by
Level 10

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

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

Avatar

Level 10

Handle the conditional logic in Sling Model.