Can I use the built-in HAML-style ruby ​​with Coffeebeans / js.coffee reviews?

The rails controller returns JS often, and I use Coffeebeans to allow me to view js.coffee. The only problem is that it uses syntax <%= ... %>for embedded Ruby ... I would ideally like to use Coffeescript / HAML style string interpolation, i.e. tags #{..}and HAML, i.e. Instead <%= ... %>, just use =the correct indentation.

I would suggest that this syntax is best used with js.coffee.haml file extensions. Is it possible? Just saving my file with this extension does not work, I assume Coffeebeans needs to be tweaked a bit to allow this, but I don't know what I need to do.

This post suggested: Chaining template handlers in Rails 3

Any suggestions on how to do this?

+5
source share
1 answer

The question you contacted had a second answer related to a blog post that came later, which is now dead ... but I found it on archive.org! The message also contains the text with the code needed in the initializer so that you can name your files ending in js.coffee_hamland process them.

In the interest of preventing future dead links, the code is given here in essence, but I did not write it and did not check if it still works:

module Coffee
  module Rails
    class HamlTemplateHandler
      def self.haml_handler
        @@haml_handler ||= ActionView::Template.registered_template_handler(:haml)
      end

      def self.call(template)
        compiled_source = haml_handler.call(template)
        "CoffeeScript.compile(begin;#{compiled_source};end)"
      end
    end
  end
end

ActiveSupport.on_load(:action_view) do
  ActionView::Template.register_template_handler :coffee_haml, Coffee::Rails::HamlTemplateHandler
end

, haml , , - coffeescript, :

if xyz == 1
  do_this y
  do_that x

:plain:

:plain
  if xyz == 1
    do_this y
    do_that x
0

All Articles