leshill / handlebars_assets

Use handlebars.js templates with the Rails asset pipeline.
MIT License
649 stars 159 forks source link

Can't get hamlbars to play nicely with handlebars_assets #85

Open timherby opened 10 years ago

timherby commented 10 years ago

I'm trying to use hamlbars with handlebars_assets, but so far have failed to get them to integrate properly.

Out of the box, I tried including both gems:

  gem 'handlebars_assets'
  gem 'hamlbars'

And when I create a file with a .hamlbars file, it gets compiled properly, but by the handlebars_assets Tilt template, not the hamlbars Template class.

If I change the extension to "file.js.hbs.hamlbars" (as described here https://github.com/jamesotron/hamlbars#chaining-compilation-using-the-rails-asset-pipeline), I get "file.js.haml.js" as my include, and "file.js" cannot even be downloaded directly.

I then decided to pull the latest version of handlebars_assets which changes the initialization order so that the hamlbars template gets used. I did this by changing my gemfile to point to the latest commit in this branch: https://github.com/leshill/handlebars_assets/tree/fix-sprockets-register as described in issue https://github.com/leshill/handlebars_assets/pull/73

  gem 'handlebars_assets', git: 'https://github.com/leshill/handlebars_assets.git', ref: '510fb66d96b881e08af67867d6773cf7a1a17ba2'
  gem 'hamlbars'

But that doesn't do the trick. If I keep the filename as "file.hamlbars" no files are included by

If I switch to "file.js.hbs.hamlbars", I run into this issue https://github.com/jamesotron/hamlbars/issues/42 (Illegal nesting: nesting within plain text is illegal). If I follow the advice in that thread, and switch "file.js.hbs.hamlbars" to ".js.hamlbars" but then the handlebars template gets returned uncompiled to the browser.

If anyone has any tips about how to use both gems together, I would really appreciate it. I have cross-posted this to the other gem's forum too (https://github.com/jamesotron/hamlbars/issues/51) for tracking.

AlexRiedler commented 10 years ago

I changed how registration is done in the fix-sprockets-register experimental branch (which is part of the next version).

In order to get it to work for templates of the name "foo.js.hbs.hamlbars":

e.g. config/initializers/templating.rb:

require 'handlebars_assets'

::HandlebarsAssets.configure do |config|
  config.haml_enabled = false
  # and/or
  config.hamlbars_extensions = []
end

I tested on Rails 3.2 locally, does this work for you?

timherby commented 10 years ago

Thanks @AlexRiedler. This worked great! I tried either one, and they both seem to do the trick.

BTW, when looking through config.rb, I noticed there may be a small typo. In the following, there is no 'hbs_extensions' registered in the attr_writer, so I assume you meant '@handlebars_extensions'

    def handlebars_extensions
      @hbs_extensions ||= ['.hbs', 'handlebars']
    end

Thanks again for the rapid fix.

AlexRiedler commented 10 years ago

@timherby, haha yeah, thanks for notifying me! and no problem!

timherby commented 10 years ago

One last thing. I tried to simplify to only disabling haml by using this setting:

  config.haml_enabled = false

but then the .hbs registration still kicks in and tries to process the template as haml because HanldeBarsTemplate.is_haml? is solely looking at the registered extensions. It should probably also look at whether haml is enabled.

For now, just using this in the configuration works fine:

  config.hamlbars_extensions = []
rpocklin commented 10 years ago

I just started using handlebars_assets yesterday with the .hamlbars extension and it works fine for me with Rails 3.2.14. I didn't include gem 'hamlbars' but i'm using gem 'haml' for my server side view templates.

I tried adding gem 'hamlbars' to my Gemfile and I got a big deprecation warning: DEPRECATION WARNING: Hamlbars 2.0 removes asset compilation! but it still worked with the original file /app/assets/javascripts/templates/file.hamlbars. I'm requiring the full 'handlebars' compiler in application.js. Hope that helps.

AlexRiedler commented 10 years ago

@rpocklin I assume this is because the separation of pipelines that you are using. (or files are in both folders/symlinked) ? the 'handlebars_assets' gem does take '.hamlbars' files and compile them... they just won't support the 'hb' helper (which is why they are slightly incompatible right now).

rpocklin commented 10 years ago

Yeah, my templates for client-side are different than the server-side views. I'm not using the hb helper - maybe I missed something but I don't see much benefit - seems just as easy to use the {{foo}} notation in the .hamlbars files.