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

Mistake in block helper example #260

Closed
nfang opened this issue Jun 22, 2012 · 5 comments
Closed

Mistake in block helper example #260

nfang opened this issue Jun 22, 2012 · 5 comments

Comments

@nfang
Copy link

nfang commented Jun 22, 2012

On this page, the code example for registering with helper is incorrect. It shows the following:

Handlebars.registerHelper(function(context, options) {
  return options.fn(context);
});

The helper name should be passed in as the first parameter:

Handlebars.registerHelper('with', function(context, options) {
  return options.fn(context);
});
@wycats
Copy link
Collaborator

wycats commented Jun 22, 2012

Can you submit a pull request :D

@alextreppass
Copy link

The example on https://github.com/wycats/handlebars.js/#registering-helpers also seems incorrect:

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>';
});
var template = Handlebars.compile(source);

var data = { "people": [
    { "name": "Alan", "id": 1 },
    { "name": "Yehuda", "id": 2 }
  ]};
template(data);
// Should render:
// <ul>
//   <li><a href="/people/1">Alan</a></li>
//   <li><a href="/people/2">Yehuda</a></li>
// </ul>

However in Chrome 19.0.1084.56 / Firefox 13.0.1 this gives the following error:

Uncaught Error: Parse error on line 2:
...l>{{#people}}<li>{{{#link}}}{{name}}{{/l
-----------------------^
Expecting 'ID', got 'undefined' 

Looks like two things are wrong -

  1. get not working in block helpers as per: __get__ not supported in block helper functions pillarjs/hbs#18
  2. the link block expression call has no first argument, so the only argument is the options hash

Re-working the example:

Handlebars.registerHelper('link', function(options) {
    return '<a href="/people/' + this.id + '">' + options.fn(this) + '</a>';
});

@alextreppass
Copy link

The 'Hash Arguments' example at http://handlebarsjs.com/block_helpers.html is also slightly incorrect:

Handlebars.registerHelper('list', function(context, options) {
    var attrs = SC.keys(options.hash).map(function(key) {
        key + '="' + options.hash[key] + '"';
    }).join(" ");

    return "<ul " + attrs + ">" + context.map(function(item) {
        return "<li>" + options.fn(item) + "</li>";
    }).join("\n") + "</ul>";
});

The attrs map function needs to return its result. Object.keys instead of SC.keys would also be more friendly to people working through the examples:

Handlebars.registerHelper('list', function(context, options) {
    var attrs = Object.keys(options.hash).map(function(key) {
        return key + '="' + options.hash[key] + '"';
    }).join(" ");

    return "<ul " + attrs + ">" + context.map(function(item) {
        return "<li>" + options.fn(item) + "</li>";
    }).join("\n") + "</ul>";
});

@wycats
Copy link
Collaborator

wycats commented Jul 9, 2012

Would you mind submitting a pull request to handlebars-site?

@wycats wycats closed this as completed Jul 9, 2012
@jednano
Copy link

jednano commented Jul 18, 2013

Were the three {{{ in your #link block helper intended?

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

No branches or pull requests

4 participants