Mustache javascript: how to handle with boolean values

非 Y 不嫁゛ 提交于 2019-12-05 11:58:16

问题


I have a javascript object obj and the value of the key can be true or false.

This value is passed to mustache template.

// javascript object

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?


回答1:


it's just like this:

<span>
    {{#like}}
        Like <!-- If {like: true} --->
    {{/like}}
    {{^like}}
        Unlike <!-- If {like: false} --->
    {{/like}}
</span>



回答2:


Just use a section and an inverted section:

{{#like}}
<span>
   Like <!-- If {like: true} --->
</span>
{{/like}}

{{^like}}
<span>
   Unlike <!-- If {like: false} --->
</span>
{{/like}}


来源:https://stackoverflow.com/questions/9114037/mustache-javascript-how-to-handle-with-boolean-values

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