Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 496
antonjs Avatar asked Feb 02 '12 14:02

antonjs


People also ask

How do you check if a boolean is true in JS?

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) .

Can a boolean be null JavaScript?

boolean can take the values of true and false . Values from other types can be truthy or falsy, like undefined or null .

What are boolean values in JavaScript?

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.


2 Answers

it's just like this:

<span>     {{#like}}         Like <!-- If {like: true} --->     {{/like}}     {{^like}}         Unlike <!-- If {like: false} --->     {{/like}} </span> 
like image 91
oezi Avatar answered Oct 15 '22 06:10

oezi


Just use a section and an inverted section:

{{#like}} <span>    Like <!-- If {like: true} ---> </span> {{/like}}  {{^like}} <span>    Unlike <!-- If {like: false} ---> </span> {{/like}} 
like image 27
Daff Avatar answered Oct 15 '22 08:10

Daff