dustjs OR condition over multiple parameters

1.4k Views Asked by At

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.

2

There are 2 best solutions below

2
Interrobang On BEST ANSWER

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}
0
rragan On

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.