Mustache javascript:如何处理布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9114037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mustache javascript: how to handle with boolean values
提问by antonjs
I have a javascript object obj
and the value of the key can be true
or false
.
我有一个 javascript 对象obj
,键的值可以是true
or false
。
This value is passed to mustache template.
这个值被传递给 mustache 模板。
// javascript object
// JavaScript 对象
obj = {
like: true // or false
}
// template
// 模板
<span>
{{ like }}
</span>
Now I would like having the result of the rendering in this way:
现在我想以这种方式获得渲染结果:
<span>
Like <!-- If {like: true} --->
</span>
<span>
Unlike <!-- If {like: false} --->
</span>
What is the best way to make it in mustache template?
在胡子模板中制作它的最佳方法是什么?
回答by oezi
it's just like this:
就像这样:
<span>
{{#like}}
Like <!-- If {like: true} --->
{{/like}}
{{^like}}
Unlike <!-- If {like: false} --->
{{/like}}
</span>
回答by Daff
Just use a section and an inverted section:
只需使用一个部分和一个反向部分:
{{#like}}
<span>
Like <!-- If {like: true} --->
</span>
{{/like}}
{{^like}}
<span>
Unlike <!-- If {like: false} --->
</span>
{{/like}}