Reputation: 8176
https://github.com/wycats/handlebars.js/#registering-helpers
In the previous "Registering Helpers" section they demonstrated creating a single-tag helper:
Handlebars.registerHelper('link_to', function(context) {
return "<a href='" + context.url + "'>" + context.body + "</a>";
});
{{#posts}} <li>{{{link_to this}}}</li> {{/posts}}
Notice that 'context' is passed in, and they are able to easily access it's properties by saying 'context.url', 'context.body', etc.
https://github.com/wycats/handlebars.js/#block-helpers
Well, in the "Block Helpers" section, they have a similar example, but it seems unnecessarily convoluted:
Handlebars.registerHelper('link', function(context, fn) {
return '<a href="/people/' + this.__get__("id") + '">' + fn(this) + '</a>';
});
{{#people}} <li>{{{#link}}}{{name}}{{/link}}</li> {{/people}}
First of all, this time they aren't passing 'this' in the opening tag, so how is the helper receiving the 'context' param? If Handlebars is passing that in automatically as a convenience, then why doesn't it do that for the single-tag helpers as well? Then the first example could just use "{{link_to}}" instead of "{{link_to this}}". What am I missing?
Secondly, if the block helper receives a 'context' param, then why can't it use that the same way as the single-tag helper? Why can't it say 'context.id' instead of the horrific 'this.get("id")'? What am I missing?
I hope this doesn't seem like picking nits in docs. Compare and contrast is the way I learn to really understand the nature of a thing. Help me replace confusion with understanding. :)
Upvotes: 1
Views: 4885
Reputation: 2811
I believe the example in the README.md is outdated, there is an issue about it #234 which is still open as of today.
Take a look at the Block Helpers examples at http://handlebarsjs.com/block_helpers.html ... it seems to be current at first glance.
Upvotes: 2