spacebars: how to use and / or in if statements

Simpanoz picture Simpanoz · Feb 10, 2015 · Viewed 9k times · Source

I have following code:

 <div class="form-group {{#if afFieldIsInvalid name='latitude' OR name='longitude'}}has-error{{/if}}">......</div>

How can I use AND/OR in if conditions of spacebars templates ?

Answer

Peppe L-G picture Peppe L-G · Feb 10, 2015

Spacebars can't handle logical expressions, so you need to create a helper handling the calculations for you.

Actually, you can achieve and functionality with nested ifs like this:

{{#if condition1}}
    {{#if condition2}}
        <p>Both condition hold!</p>
    {{/if}}
{{/if}}

And or like this:

{{#if condition1}}
    <p>One of the conditions are true!</p>
{{else}}
    {{#if condition2}}
        <p>One of the conditions are true!</p>
    {{/if}}
{{/if}}

But I would prefer using a helper.