I want to check in Mustache if a value (not a boolean) is equal to a string. I saw in a random tutorial that you can use a function to define the condition but I haven't seen any documention on the syntax for that. Does anyone know how to do it?
Asked
Active
Viewed 9,618 times
0
-
1The whole point of Mustache is "logic-less templates." Couldn't you evaluate the equality with Javascript and pass the boolean on to Mustache? – Theron Luhn Apr 23 '12 at 17:30
2 Answers
4
The workaround is to simply create a wrapping condition variable that is evaluated outside of the mustache template, as you may already know.
var displaySection = false;
if(typeof input == 'string'){
displaySection=true;
}
And then wrap whatever you want to do in a section tag
{{#displaySection}}

Kzqai
- 22,588
- 25
- 105
- 137
1
As Theron said, Mustache is a logic-less templating library and cannot handle conditions (you should use javascript beforehand for that).
If you really want to handle conditional blocks in your templates you should check out http://handlebarsjs.com/.

Alexis C.
- 4,898
- 1
- 31
- 43
-
3Just need to add that Handlebars is also logic-less and you'll need to make a [cheat](http://stackoverflow.com/questions/8853396/logical-operator-in-a-handlebars-js-if-conditional) to implement this functionality – Dmitry Aug 07 '13 at 13:32