Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Exposed current Context to lambdas via Mustache.currentContext #260

Open
wants to merge 1 commit into
from

Conversation

Projects
None yet
1 participant

techhead commented Oct 1, 2012

This is an optional feature that does not break compatibility unless you use it ;)

I would like to see these properties added to the Mustache spec at some point in the future if consensus can be reached on the best way to expose them, namely the current index of an item in an array and a reference to that array (see the definition of the forEach callback, https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach). But this patch also exposes the entire current context stack to a lambda.

It is NOT my suggestion that a global property named Mustache.currentContext be defined for the spec but that the spec should define a way in which to expose this information.

See test/_files/current_context* for examples on utility.

current_context_oddeven.js

({
    rows: [1,2,3],
    oddeven: function() {
        var ctx = Mustache.currentContext;
        return (ctx.index % 2) ? 'even' : 'odd';
    }
})
<table>
{{#rows}}
<tr class="{{oddeven}}"><td>{{.}}</td></tr>
{{/rows}}
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment