Check with mustache js if parameter is a specific value

僤鯓⒐⒋嵵緔 提交于 2019-11-27 01:51:10

问题


Is it possible to check in mustache js for a specific value like {{name}} == "James" ?

DATA:

json: {
    name: "James"
}

HTML:

{{name}} //Will give me James as output
{{name == "James" }} //Is it possible to check specific value?

回答1:


No. The idea behind mustache is that it is a logic-less templating syntax. So, no, such a logic is not possible.

We call it "logic-less" because there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values. https://github.com/janl/mustache.js




回答2:


Although the question was answered I just have one thing to add (and it's a bit too long for a comment). As Harri pointed, such logic is indeed not possible. However, a cool thing I use quite often with Mustache is testing for true and false. When you are "constructing" Mustache json object, prepare the logic you'll be needing in the template. For instance in your case, if the object is as follows:

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

Then in the template you can have:

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

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


来源:https://stackoverflow.com/questions/20345288/check-with-mustache-js-if-parameter-is-a-specific-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!