Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you register a custom helper if you are precompiling a custom helper in Handlebars.js?

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?

like image 631
wusher Avatar asked Feb 03 '12 01:02

wusher


People also ask

How do I register Handlebars in 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.

What is helpers in Handlebars?

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.

Which built in helper method is the inverse of `# If `?

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.

What are Handlebars options?

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.


1 Answers

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

like image 145
Chris Biscardi Avatar answered Sep 27 '22 18:09

Chris Biscardi