Search code examples
conditional-statementsdust.js

How do I write an 'AND' condition in dust?


In my dust file, I have something like this :

<div class="{?cond1}hidden{/cond1}" role="alert">
{@localize key="warning" eng="Warning: Not allowed" /}
</div>

I would like to make the element hidden if cond1 AND cond2 = true. How do I add one more condition inline here ?


Solution

  • In your example, just wrap the classname in two conditions-- that's the equivalent of an AND.

    <div class="{?cond1}{?cond2}hidden{/cond2}{/cond1}" role="alert">
      {@localize key="warning" eng="Warning: Not allowed" /}
    </div>
    

    Should your logic become more complicated, you can move the condition check out of the template and into your context. Simply write a function that does the evaluation and returns true or false:

    {
      cond1: true,
      cond2: false,
      cond3: 'a',
      warningIsHidden: function(chunk, context) {
        return context.get('cond1') === true &&
               context.get('cond2') === true &&
               context.get('cond3') !== 'b';
      }
    }