spacebars: how to use and / or in if statements
Asked Answered
J

6

5

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 ?

Jesseniajessey answered 10/2, 2015 at 8:9 Comment(1)
possible duplicate of Meteor.js Handlebars template logic operatorsLiquidation
L
5

Spacebars is an extension of Handlebars, which is designed to be a logic-less template language.

The solution is to register a helper. For the general case, see these similar questions:

To define helpers in Meteor, use Template.registerHelper

Liquidation answered 10/2, 2015 at 8:18 Comment(0)
D
10

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.

Delija answered 10/2, 2015 at 8:18 Comment(0)
L
5

Spacebars is an extension of Handlebars, which is designed to be a logic-less template language.

The solution is to register a helper. For the general case, see these similar questions:

To define helpers in Meteor, use Template.registerHelper

Liquidation answered 10/2, 2015 at 8:18 Comment(0)
S
0

You have an extension designed for this case.

Raix Handlebars

You can use for an 'AND' condition something like this:

{{#if $in yourVariable 'case1' 'case2' }}
      Your code Here
{{/if}}
Serajevo answered 22/11, 2016 at 8:27 Comment(0)
S
0

Taking the solution one step further. This adds the compare operator.

Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {

    switch (operator) {
        case '==':
            return (v1 == v2) ? options.fn(this) : options.inverse(this);
        case '===':
            return (v1 === v2) ? options.fn(this) : options.inverse(this);
        case '!=':
            return (v1 != v2) ? options.fn(this) : options.inverse(this);
        case '!==':
            return (v1 !== v2) ? options.fn(this) : options.inverse(this);
        case '<':
            return (v1 < v2) ? options.fn(this) : options.inverse(this);
        case '<=':
            return (v1 <= v2) ? options.fn(this) : options.inverse(this);
        case '>':
            return (v1 > v2) ? options.fn(this) : options.inverse(this);
        case '>=':
            return (v1 >= v2) ? options.fn(this) : options.inverse(this);
        case '&&':
            return (v1 && v2) ? options.fn(this) : options.inverse(this);
        case '||':
            return (v1 || v2) ? options.fn(this) : options.inverse(this);
        default:
            return options.inverse(this);
    }
});

Use it in a template like this:

{{#ifCond name='latitude' '||' name='longitude'}}
Subjectivism answered 10/10, 2017 at 13:57 Comment(0)
W
0

You can do that by using if-else syntax

<div class="form-group {{#if afFieldIsInvalid}} latitude {{else}} longitude {{/if}} has-error">...</div>
Wanda answered 31/3, 2020 at 15:35 Comment(0)
M
-6

instead of using 'OR' try '||'. Or define a method in a javascript file.

Milurd answered 10/2, 2015 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.