2

I have an object with multiple properties viz propA, propB propC and propD. I want to write a condition with OR checking more than one parameter like below.

{@if cond="'{obj.propA}'.length > 0 || '{obj.propB}'.length > 0 || '{obj.propC}'.length> 0} ... {/if}

Now since @if is deprecated in dust, how do i write an equivalent of this with eq or select. Or is there a new helper i can utilize for such scenarios.

Sripaul
  • 2,227
  • 9
  • 36
  • 60

2 Answers2

3

I'm assuming that the props you're testing are strings.

This example requires dustjs-helpers >= 1.6.

You can use the {@any} and {@none} helpers mentioned by @rragan like this:

{@select}
  {@ne key=obj.propA value="" /}
  {@ne key=obj.propB value="" /}
  {@ne key=obj.propC value="" /}
  {@any}At least one of the above tests was true. At least one prop is not an empty string.{/any}
  {@none}None of the tests above passed. All the props are empty{/none}
{/select}
Interrobang
  • 16,984
  • 3
  • 55
  • 63
  • exactly what i was looking for. Thanks a lot. Also found the same answer here http://stackoverflow.com/questions/29140924/how-to-run-or-condition-in-dust-template. – Sripaul Oct 27 '15 at 18:30
  • 1
    I should also add that if you have to do this a lot, you should write a context helper to do this work for you. – Interrobang Oct 27 '15 at 20:47
2

select was recently extended with @any and @none that let you do multiple OR logic. Note that the .length only works because the deprecated @if uses eval. Dust tests use existence/non-existence so I think you can avoid using .length.

If you still prefer @if, see https://github.com/rragan/dust-motes/tree/master/src/helpers/control/if for an interpretive version of it that does not use eval.

rragan
  • 484
  • 2
  • 2