Martin Erlic
Martin Erlic

Reputation: 5665

How can I iterate over a list of objects in Handlebars.js with two if conditions?

I'm trying to iterate over a list in Handlebars.js with two if conditions. How can I get this to work?

{{#each blog}}
// first condition is string and second is boolean
    {{#ifEquals nominatorRegion == "Region1" && accepted == "true"}}
        ...stuff here
    {{/ifEquals}}
{{/each}

Upvotes: 0

Views: 200

Answers (2)

yeoman
yeoman

Reputation: 1681

The whole point of mustache & handlebars is to have no logic at all in your templates, except for conditional values, and lists of values 😊

Reason: logic is harder to read, maintain, and debug in a template language.

The trick is to create a model in code that takes care of all the ifs and other complex matters, which then can be processed by mustache / habdlebars with only optionals and lists 😊

Therefore, simply only include something in the model, i.e. the value / data structure / json that is passed to mustache / handlebars if the condition holds, in your code.

Your code, that is, in an actual programming language, like js, swift, C++, &c.

Upvotes: 1

Martin Erlic
Martin Erlic

Reputation: 5665

I was able to do this by embedding another ifEquals statement:

{{#each blog}}
    {{#ifEquals nominatorRegion "Region1"}}
        {{#ifEquals accepted false}} // make sure boolean is not enclosed in quotations
            ...
        {{/ifEquals}}
    {{/ifEquals}}
 {{/each}}

Upvotes: 0

Related Questions