简体   繁体   中英

Mustache javascript: how to handle with boolean values

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?

it's just like this:

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

Just use a section and an inverted section:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM