aemsightlyhtl

Ternary operator doesn't work for data-sly-list in HTL?


I am trying to set a list to be either of the member list from two different objects obj1 and obj2, and the obj1 takes priority over obj2. So I have the following code:

data-sly-list=${obj1.someList ? obj1.someList : obj2.someList}

But when I run the application I get a parsing error as such:

no viable alternative at input '<EOF>' for expression ${obj1.someList?

As far as I know, the ternary operator works for non data-sly-list htl statements. So is there a work around this or am I doing something wrong?


Solution

  • If the code you posted is indeed like this you seem to be missing quotes:

    Your code:

    data-sly-list=${obj1.someList ? obj1.someList : obj2.someList}
    

    With quotes:

    data-sly-list="${obj1.someList ? obj1.someList : obj2.someList}"
    

    In addition I would advise you to do such logic in your Sling Model. I know it is tempting to do this in HTL but one of the reasons we use template languages like HTL is to decouple business logic from our views.

    This code is also not testable by unit tests. So there is another good reason to move this expression into your Sling Model.