Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposed current Context to lambdas via Mustache.currentContext #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

techhead
Copy link

@techhead 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant