小胡子模板 - 检查 json 是否包含特定值

mustache templating- to check if json contains particular value

在 Mustache 模板中,我想检查 json 中是否存在特定键。我怎样才能做到这一点?

Mu​​stache 用于无逻辑模板,因此您的代码中不能有这种情况。

您需要以某种方式修改 JSON,可以是在创建它时(如果您可以控制它),也可以是在您收到它之后。例如,您可以向特定的 JSON 对象添加 "isTraining" 属性:

    {
        "channelId": "training",
        "maxResults": 99,
        "searchRadius": 100,
        "isTraining": true
    }

然后您可以在 Mustache 模板中使用它:

{{#isTraining}}
    ...
{{/isTraining}}

小胡子毫无逻辑。这是您可以在控制器中执行的逻辑。

类似问题:

How to handle an IF STATEMENT in a Mustache template?

Check with mustache js if parameter is a specific value

Khalid Dabjans 回答的例子:

json: {
    name: "James",
    isJames: true
}

并且在 HTML 中:

{{#isJames}}
    //the name is James
{{/isJames}}

{{^isJames}}
    //the name is NOT James
{{/isJames}}