Spacebars: how to use and / or in if statements

I have the following code:

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

How can I use AND / OR if the conditions for spatial bar patterns?

+4
source share
5 answers

Spacebars is a Handlebars extension that is designed to be used as a language without logic.

The solution is to register an assistant. In general, see Related Questions:

To define helpers in Meteor, use Template.registerHelper

+2
source

, .

and ifs :

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

or :

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

.

+8

, .

Raix Handlebars

"" - :

{{#if $in yourVariable 'case1' 'case2' }}
      Your code Here
{{/if}}
0

. .

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);
    }
});

:

{{#ifCond name='latitude' '||' name='longitude'}}
0

'OR' try '||'. javascript.

-5

All Articles