I am trying to precompile a Handlebars.js template app/views/templates/walrus.handlebar
with the command handlebar app/views/templates/walrus.handlebar
but it fails because the template uses a custom helper that I have defined in a seperate js file public/javascripts/handlebar_helpers.js
.
How do I call the command line version of Handlebars so it is aware of the javascript file with the custom helper?
Handlebars. registerHelper("noop", function(options) { return options. fn(this); }); Handlebars always invokes helpers with the current context as this , so you can invoke the block with this to evaluate the block in the current context.
Helpers can be used to implement functionality that is not part of the Handlebars language itself. A helper can be registered at runtime via Handlebars. registerHelper , for example in order to uppercase all characters of a string.
You can use the unless helper as the inverse of the if helper. Its block will be rendered if the expression returns a falsy value. If looking up license under the current context returns a falsy value, Handlebars will render the warning. Otherwise, it will render nothing.
When you use this syntax, Handlebars passes an options parameter to your helper as the last argument. The options object contains a fn method that works just like a compiled template... var html = options. fn(context); gives you the rendered template from inside the block.
handlebars <input> -f <output> -k <helper>
It's in the docs here: http://handlebarsjs.com/precompilation.html
Edit March 2014:
For people having issues reading the docs, here's an example for custom helper "fullname"
handlebars myTemplate.handlebars -f handlebars-fullname.js -k fullname
with this helper:
Handlebars.registerHelper('fullname', function(person) {
return person.firstName + " " + person.lastName;
});
You still have to include the helper in the page with the handlebars.runtime.js
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