【发布时间】:2015-09-08 07:23:19
【问题描述】:
在 Mustache 模板中,我想检查 json 中是否存在特定键。我怎样才能做到这一点?
【问题讨论】:
-
考虑使用函数
在 Mustache 模板中,我想检查 json 中是否存在特定键。我怎样才能做到这一点?
【问题讨论】:
Mustache 用于无逻辑模板,因此您的代码中不能有这种条件。
您需要以某种方式修改 JSON,无论是在您创建它时(如果您可以控制它),还是在您收到它之后。例如,您可以向该特定 JSON 对象添加“isTraining”属性:
{
"channelId": "training",
"maxResults": 99,
"searchRadius": 100,
"isTraining": true
}
然后你可以在 Mustache 模板中使用它:
{{#isTraining}}
...
{{/isTraining}}
【讨论】:
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}}
【讨论】: