Skip to content

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.

1 participant