简体   繁体   中英

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}

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, ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM