3

In the Sightly templating language, for Adobe AEM6, how do I use a specific class if a condition is true?

${properties.reduceImage} is my checkbox, so if the checkbox is checked then add the class if not then it doesn't return anything. I'm not sure if I'm doing this the correct way.

<div data-sly-test="${properties.reduceImage}" data-sly-unwrap>
    <div class="reduce-image">
    </div>
</div>
Michael Shopsin
  • 2,055
  • 2
  • 24
  • 43
Code4life
  • 67
  • 1
  • 1
  • 11

1 Answers1

14

The expression language of Sightly has a few operators that allow to do things like that.

In your case, you have two possibilities: the conditional operator, or the logical AND (&&) operator.

Conditional operator

This operator works like data-sly-test, but at the level of the expression. What is before the question mark (?) is the condition, and then come two parts, separated with a column (:). The first part is the output if the condition is true, and the second part is the output if the condition is false (which we leave empty in your example).

<div class="${properties.reduceImage ? 'reduce-image' : ''}">
</div>

Logical AND operator

This writing is a bit shorter, but also less explicit in it's intention. It uses the fact that like in JavaScript, ${value1 && value2} returns value1 if it is falsy (e.g. false, or an empty string), otherwise it returns value2:

<div class="${properties.reduceImage && 'reduce-image'}">
</div>

In both examples the class attribute will be removed altogether if the corresponding condition is false, because Sightly does remove all attributes with expression that end up being empty or false.

Here's the full documentation of Sightly's expression language:
http://docs.adobe.com/docs/en/aem/6-1/develop/sightly/expression-language.html

Gabriel Walt
  • 1,629
  • 17
  • 19
  • Thank you Gabriel, You've really explained everything clearly. Thank you for the documentation as well. I'm going to study this all month. – Code4life Sep 14 '15 at 14:51
  • When I implement this the css doesn't show. I names the css file. image-reduce.css and added a width decrease to a specific selector. When I check the checkbox nothing happens. – Code4life Sep 14 '15 at 16:26
  • And when you output just your property, do you see it change based on your dialog? E.g:

    ${properties.reduceImage}

    – Gabriel Walt Sep 14 '15 at 17:35
  • To be honest, I don't understand what you try to explain. Maybe your property gets stored as a string, instead of a boolean. And the string "false" is evaluated to true by Sightly. If that's the case, you'd have to either adjust your dialog to store the value as a boolean, or to add some logic with Sightly's Use-API when reading the value. Else, there's not much I can help you, but make sure to very systematically debug the whole thing, and make sure the browser or the server doesn't cache any previous version of your CSS. – Gabriel Walt Sep 14 '15 at 18:05
  • How to combine 2 if else statements in sightly, in javascript you can do this by (expr1 ? (expr2 ? xx : bb) : cc) – Sytham Aug 29 '18 at 11:54
  • @GabrielWalt I need help to do this ${properties.title=='hompage'?'primary':' '} is it allowed? – Oliver May 20 '19 at 06:06