36

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?

1
  • It's the same idea as with 2 parameters. This answer would allow you to use any JS expression. Commented Dec 20, 2016 at 21:54

3 Answers 3

37

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}}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @BarlasApaydin
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.
The answer to this question solves it directly: stackoverflow.com/questions/8853396/…
how does this answer equivalent to {{#if A || B || C}} something {{/if}} ? isn't it equivalent to {{#if A && B && C}} something {{/if}} ?
8

What about this?

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

1 Comment

This should be the chosen answer as it gives an alternative solution when the OR-functionality is not implemented in the language
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.