Handlebars_assets

screenshot of Handlebars_assets

Use handlebars.js templates with the Rails asset pipeline.

Overview

The article discusses the use of handlebars.js templates with the Rails asset pipeline and/or sprockets. It introduces the handlebars_assets gem, which allows for the compilation, compression, and caching of handlebars templates as part of the Rails 3.1 asset pipeline. The gem also works with frameworks like Sinatra. The article mentions some breaking changes in the repository and provides installation instructions for Rails 4.0+ and Rails 3.1+, as well as for using handlebars_assets with sprockets in non-Rails projects. It also mentions the inclusion of handlebars.runtime.js in the JavaScript manifest and the location of the templates directory.

Features

  • Handlebars templates are compiled, compressed, and cached as part of the Rails 3.1 asset pipeline.
  • Works with frameworks like Sinatra or earlier versions of Rails.
  • Can be used for precompiling assets.
  • Handles multiple extensions for the same compilation pipeline.

Rails 4.0+

  1. Load handlebars_assets in your Gemfile.
  2. Follow the JavaScript Setup instructions.

Rails 3.1+

  1. Load handlebars_assets in your Gemfile as part of the assets group.
  2. Follow the JavaScript Setup instructions.

Sprockets (Non-Rails)

  1. Load handlebars_assets in your Gemfile.
  2. Add the HandlebarsAssets.path to your Sprockets::Environment instance.

JavaScript Setup

  1. Require handlebars.runtime.js in your JavaScript manifest (e.g. application.js).
  2. If required, also include handlebars.js for compiling JavaScript templates in the browser.

Templates directory

Place your templates in a directory like app/assets/javascripts/templates. In your JavaScript manifest, use require_tree to include the templates.

Rails Asset Precompiling

handlebars_assets also works with asset precompilation. Use the command rake assets:precompile.

Summary

The article discusses the handlebars_assets gem, which enables the smooth integration of handlebars.js templates with the Rails asset pipeline and/or sprockets. It provides installation instructions for different versions of Rails and alternative frameworks, as well as guidance on including the templates and configuring the JavaScript setup. The gem also works with asset precompilation.