【问题标题】:mustache templating- to check if json contains particular valuemustache 模板 - 检查 json 是否包含特定值
【发布时间】:2015-09-08 07:23:19
【问题描述】:

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

【问题讨论】:

  • 考虑使用函数

标签: json mustache


【解决方案1】:

Mustache 用于无逻辑模板,因此您的代码中不能有这种条件。

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

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

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

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

【讨论】:

    【解决方案2】:

    Mustache 是无逻辑的。这是您可以在控制器中执行的逻辑。

    类似问题:

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

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 2012-10-17
      • 2020-05-24
      • 1970-01-01
      • 2018-03-12
      相关资源
      最近更新 更多