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?
An alternative approach is to use the logical OR (||) operator. To check if a value is of boolean type, check if the value is equal to false or equal to true , e.g. if (variable === true || variable === false) .
boolean can take the values of true and false . Values from other types can be truthy or falsy, like undefined or null .
In JavaScript, a boolean value is one that can either be TRUE or FALSE. If you need to know “yes” or “no” about something, then you would want to use the boolean function. It sounds extremely simple, but booleans are used all the time in JavaScript programming, and they are extremely useful.
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}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With