handlebars - is it possible to access parent context in a partial?
Just in case anyone stumbles across this question. This functionality exists now in Handlebars.
Do this:
{{#each items}} {{! Will pass the current item in items to your partial }} {{> item-template this}} {{/each}}
Working fiddle (inspired by handlebars pull request #385 by AndrewHenderson) http://jsfiddle.net/QV9em/4/
Handlebars.registerHelper('include', function(options) { var context = {}, mergeContext = function(obj) { for(var k in obj)context[k]=obj[k]; }; mergeContext(this); mergeContext(options.hash); return options.fn(context);});
Here's how you'd setup the parent template:
{{#each items}} {{#include parent=..}} {{> item-template}} {{/include}}{{/each}}
And the partial:
value is {{parent}}
As of 2.0.0 partials now supports passing in values.
{{#each items}} {{> item-template some_parent_var=../some_parent_var}}{{/each}}
Took me awhile to find this, hope it's useful for someone else too!