tiny-mustache
tiny-mustache copied to clipboard
[bug] Lambda functions return a function, not a value
When comparing the test for tiny-mustache against the results of mustache.js, I noticed there is a bug in the current lambda
implementation.
The Mustache manual states:
Lambdas
When the value is a callable object, such as a function or lambda, the object will be invoked and passed the block of text. The text passed is the literal block, unrendered.
{{tags}}
will not have been expanded - the lambda should do that on its own. In this way you can implement filters or caching.Template:
{{#wrapped}} {{name}} is awesome. {{/wrapped}}
Hash:
{ "name": "Willy", "wrapped": function() { return function(text, render) { return "<b>" + render(text) + "</b>" } } }
Output:
<b>Willy is awesome.</b>
However, instead of a function that returns a function, tiny-mustache expects a plain function (i.e. a function that returns a scalar).
A naive fix would be to change
https://github.com/aishikaty/tiny-mustache/blob/ec53eedf187114996a0ef984a31ddf9b4cf19d10/mustache.js#L45
to
output += name().call(ctx, childCode, function (template) {
but that would break backward compatibility. A check could be added, but I am not sure how that matches the desire to keep the code as small as possible.