Say I have a static list of users cached somewhere in my application like App.Users. I'll probably have the need to list my users in several dozen places in my application. Conventionally, I'll just need to pass my list in with my context to the template.
var tmpl = Handlebars.templates['TemplateName'];
var html = tmpl({
model: model,
users: App.Users
});
But this approach requires some wiring in both the template and the javascript. What I would like to do is specify this in the template alone so I don't need to remember this in my scripts. Consider something like this...
{{#each {{users}}}}
<li> ... </li>
{{/each}}
...Where users is a helper function that just returns my App.Users. Wouldn't that be nice?
So that totally doesn't compile. What is another solution?
Went with an abstract helper function deal... which let's be honest, seems to be the solution to 99% of Handlebars questions.
Handlebars.registerHelper('global', function(context, options) {
return options.fn(App.[context].toJSON()); // Object is Backbone Collection
})
And used in an example...
{{#global "Users"}}
{{#each this}}
<th>{{Name}}</th>
{{/each}}
{{/global}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With