简体   繁体   English

如何使用两个if条件遍历Handlebars.js中的对象列表?

[英]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. 我试图用两个if条件遍历Handlebars.js中的列表。 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 😊 胡子和把手的全部要点是,除了条件值和值列表外,模板中完全没有逻辑logic

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 😊 诀窍是在代码中创建一个模型,该模型可以处理所有if和其他复杂问题,然后可以由胡子/杂物栏处理,并且仅包含可选项和列表。

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. 因此,只需在模型中仅包含一些内容即可,例如,如果条件成立,则将值/数据结构/ json传递给mustache / handbars。

Your code, that is, in an actual programming language, like js, swift, C++, &c. 您的代码,即使用实际的编程语言,例如js,swift,C ++和&c。

I was able to do this by embedding another ifEquals statement: 我可以通过嵌入另一个ifEquals语句来做到这一点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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