43

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?
falsarella
  • 12,217
  • 9
  • 69
  • 115
rubin
  • 687
  • 2
  • 7
  • 12

2 Answers2

65

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}}
Khalid Dabjan
  • 2,697
  • 2
  • 24
  • 35
  • 6
    That's a nice tip, this is what I was after. Although originally my data is not formatted this way, I can easily pre-process the logic into it before handling it to mustache – Hamish Rouse Nov 11 '15 at 18:13
31

No. The idea behind mustache is that it is a logic-less templating syntax. So, no, such a logic is not possible.

We call it "logic-less" because there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values. https://github.com/janl/mustache.js

Harri
  • 2,692
  • 2
  • 21
  • 25
  • 2
    i have read that but you can use {{#list.length}} and {{"#list.1}} //pos 1 in array, so maybe it will work with if and so on.... – rubin Dec 03 '13 at 10:06