You can use Slim (or Haml) to serve your Batman.js templates in the Rails Asset Pipeline.
1. Include Slim in your gemfile
Add this line to your Gemfile
:
1
|
|
2. Register the Slim engine with Rails
Make a new initializer (eg, config/initializers/slim_assets.rb
) and put this in it:
1
|
|
Credit: Dillon Buchanan
Update: Serving compiled Slim (or Haml) assets in production
When you deploy to production, if you have config.initialize_on_precompile = false
, an initializer isn’t going to work. You’ll have to register the Sprockets engine another way. Add it to the application config in config/application.rb
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Now, you’ll have the Slim template engine available even if your app doesn’t intialize, which is most notably the case for bundle exec rake assets:precompile
. Nice!
3. Beef up your BatmanController
At time of writing, the batman-rails
-generated BatmanController won’t work right in production. There’s an outstanding PR with a fix. If that’s closed, then we’re good to go. In the mean time, Make your app/controllers/batman_controller.rb
look like this:
1 2 3 4 5 6 7 8 9 10 11 |
|
When this is done, any files ending in .html.slim
in assets
will be served as rendered HTML in response to XHR (Ajax) requests. Note that all non-XHR requests will still receive the batman
layout (but then Batman will respond to the request, so you’re all good).