-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Comments
Can you submit a pull request :D |
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:
Looks like two things are wrong -
Re-working the example: Handlebars.registerHelper('link', function(options) {
return '<a href="/people/' + this.id + '">' + options.fn(this) + '</a>';
}); |
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>";
}); |
Would you mind submitting a pull request to handlebars-site? |
Were the three |
On this page, the code example for registering with helper is incorrect. It shows the following:
The helper name should be passed in as the first parameter:
The text was updated successfully, but these errors were encountered: