Check with mustache js if parameter is a specific value

rubin picture rubin · Dec 3, 2013 · Viewed 33.7k times · Source

Is it possible to check in mustache js for a specific value like {{name}} == "James" ?

DATA:

json: {
    name: "James"
}

HTML:

{{name}} //Will give me James as output
{{name == "James" }} //Is it possible to check specific value?

Answer

Khalid Dabjan picture Khalid Dabjan · Dec 4, 2013

Although the question was answered I just have one thing to add (and it's a bit too long for a comment). As Harri pointed, such logic is indeed not possible. However, a cool thing I use quite often with Mustache is testing for true and false. When you are "constructing" Mustache json object, prepare the logic you'll be needing in the template. For instance in your case, if the object is as follows:

json: {
    name: "James",
    isJames: true
}

Then in the template you can have:

{{#isJames}}
    //the name is James
{{/isJames}}

{{^isJames}}
    //the name is NOT James
{{/isJames}}