Reputation: 705
I have been trying for about an hour to get a lambda to work with the mustache.js demo. Nearly all of the examples online which demonstrate lambdas, do so by declaring the json inline with their js.
I've tried the following JSON:
{
"planet": "The be the name",
"lambda": "function() {return \"{{planet}}\"}"
}
with the following mustache:
{{lambda}}
.. with no success. Is there a way to use the mustache demo page to demonstrate lambdas?
I appreciate any help.
Upvotes: 1
Views: 5060
Reputation: 1075
I declare my lambda in my js code. After I've catched the JSON from server and put it in myVar
, I do something like the following, prior to calling Mustache.render
:
myVar.myLambda = function(text,render) {
return '<b>'+render(text)+'<b>';
}
render
renders the mustache variable passed in brackets, which is the text
parameter for the lambda function, and you should pass it in your HTML like this:
<div class="blah"><p>The planet is: {{#myLambda}}{{planet}}{{/myLambda}}</p></div>
Upvotes: 1
Reputation: 5117
One does not. It's not possible to represent a lambda as JSON. Your example is just two strings, one of which happens to be a function if you eval()
it. This would work:
{
"planet": "World",
"lambda": function() { return "{{planet}}" }
}
... except that it's invalid JSON so it won't :(
Upvotes: 1
Reputation: 10429
It looks like lambdas are not supported on the demo page even though they work using the library normally. Probably an oversight, but I could be wrong.
Upvotes: 1