34

I didn't find this was possible in Handlebars... I need something like this:

{{#if A || B || C}} something {{/if}}

Is that possible to achieve? I have looked at this answer, but as I need for 3 variables (A, B, C) I don't really know how to apply it. Any ideas?

Community
  • 1
  • 1
zwiebl
  • 685
  • 2
  • 11
  • 24
  • It's the same idea as with 2 parameters. [This answer](http://stackoverflow.com/a/21915381/5743988) would allow you to use any JS expression. – 4castle Dec 20 '16 at 21:54

3 Answers3

34

They do not have multiple conditions. But you can achieve it by nesting. This works:

{{#if A}}
   {{#if B}}
     {{#if C}}
       something 
    {{/if}}
   {{/if}}
{{/if}}
Fabian Lauer
  • 8,891
  • 4
  • 26
  • 35
Harsha Vardhini
  • 692
  • 6
  • 10
  • Thanks @BarlasApaydin – Harsha Vardhini Nov 10 '17 at 05:23
  • 20
    this handles {{#if A && B && C}} but not {{#if A || B || C}} really to achieve the "or" scenario it would be {{#if A}}something{{/if}}{{#if B}}something{{/if}}{{#if C}}something{{/if}} but that's not as DRY (don't repeat yourself) because you have to repeat "something" 3 times, where "something" could be a large chunk of code. – Rich Finelli Dec 04 '17 at 17:59
  • 1
    The answer to this question solves it directly: https://stackoverflow.com/questions/8853396/logical-operator-in-a-handlebars-js-if-conditional?noredirect=1&lq=1 – Rich Finelli Dec 05 '17 at 16:06
  • 1
    how does this answer equivalent to {{#if A || B || C}} something {{/if}} ? isn't it equivalent to {{#if A && B && C}} something {{/if}} ? – yeahman Mar 24 '19 at 05:41
5

What about this?

{{#if A}}
  something
{{else if B}}
  someting B
{{else if C}}
  someting C
{{/if}}
Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Mario Mixtega
  • 96
  • 1
  • 3
  • This should be the chosen answer as it gives an alternative solution when the OR-functionality is not implemented in the language – dasj19 Dec 20 '19 at 13:00
2

in 3.0 you can do it with

{{#if A}}
  something
  {{else if B}}
    something
    {{else if C}}
      something
    {{/if}}
  {{/if}}
{{/if}}

if you use something below 3.0 you can make multiple ifs

{{#if A}}
{{else}}
  {{#if B}}

... and so on hope this helps

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Gabriel
  • 89
  • 1
  • 5