HandlebarsExec

Bindings for Handlebars.js using execjs. Tested using mini_racer, therubyracer, and native nodejs.

Usage

Simple Templates

handlebars = HandlebarsExec::Context.new
template = handlebars.compile("My Name is {{name}}")
template.template(name: "Slim Shady") #=> "My Name is Slim Shady"

Partials

Partials can be added by either adding a string with the partial or by adding a file with the partial registration in it. Referencing a file allows you to use the same files for handlebars on the backend and frontend.

# passed as a string
handlebars.register_partial("my_partial", "{{foo}} {{bar}}")

# passed as a file location
handlebars.register_partial_file(partial_file_path)

Helpers

Helpers can be added by either adding a string with the helper or by adding a file with the helper registration in it. Referencing a file allows you to use the same files for handlebars on the backend and frontend.

# passed as a string 
handlebars.register_helper("formatPhoneNumber", <<-HELP)
  function(phoneNumber) {
    phoneNumber = phoneNumber.toString();
    return "(" + phoneNumber.substr(0,3) + ") " + phoneNumber.substr(3,3) + "-" + phoneNumber.substr(6,4);
  }
HELP

# passed as a file location
handlebars.register_partial_file(partial_file_path)

Test

Testing in different JS runtimes are handled using Appraisal. Once installed, the following will setup the different js runtime test setups

appraisal install

Tests can then be run like so:

appraisal rake