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

block helper example in readme is incorrect #234

Closed
defunctzombie opened this issue May 3, 2012 · 2 comments
Closed

block helper example in readme is incorrect #234

defunctzombie opened this issue May 3, 2012 · 2 comments

Comments

@defunctzombie
Copy link

The readme shows the following:

var source = "<ul>{{#people}}<li>{{{#link}}}{{name}}{{/link}}</li>{{/people}}</ul>";
Handlebars.registerHelper('link', function(context, fn) {
  return '<a href="/people/' + this.__get__("id") + '">' + fn(this) + '</a>';
});

I had to chant it to the following to get it to work:

var source = "<ul>{{#people}}<li>{{#link this}}{{name}}{{/link}}</li>{{/people}}</ul>";
Handlebars.registerHelper('link', function(context, fn) {
  return '<a href="/people/' + this.id + '">' + fn(this) + '</a>';
});

Is this the correct new behavior?

@jmm
Copy link

jmm commented May 5, 2012

In addition to the triple braces and __get__ issues, there's the issue that I filed a ticket (#231) about. If you don't provide an argument to the helper in the template, then the helper function doesn't receive 2 arguments. You mentioned one way to get it to work, explicitly passing the current context as the argument in the template ({{#link this}}). An alternative is to change the helper to account for the options object being the first (and only) argument, e.g.

Handlebars.registerHelper( 'link', function( fn ) {

or

Handlebars.registerHelper( 'link', function( context, fn ) {

    fn = Array.prototype.slice.call( arguments, -1 )[0];

@kpdecker
Copy link
Collaborator

kpdecker commented Apr 7, 2013

The docs were updated a while back to show the options.fn syntax. I just ran them through a test and the rendering of the example matched the expectation.

https://github.com/wycats/handlebars.js/blob/master/README.markdown#block-helpers

@kpdecker kpdecker closed this as completed Apr 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants