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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

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
Community Advisor

@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

Community Advisor

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

Correct answer by
Community Advisor

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

Avatar

Level 2

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