Question

I need to be using double quotes on my attributes due to a Javascript framework issue I am having, and I have tried setting the Haml::Template.options hash as the documentation recommends when using Rails, however isn't taking affect for the 'assets' folder, regardless of where I am setting the option. Note, it is working on normal ActionView templates being rendered by Rails controllers, but not in templates I have under {Rails.root}/app/assets/javascripts/templates/*.html.haml

This is what I have in {Rails.root}/config/initializers/haml.rb:

Haml::Template.options[:attr_wrapper] = '"'

# Adds the ability to use HAML templates in the asset pipeline for use with
# Batman.js partials
Rails.application.assets.register_mime_type 'text/html', '.html'
Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate

I've also tried changing the register_engine to use Haml::Engine and Haml::Template, which both render, but still don't take my options I set above.

How do I set the Haml options for rendering in the asset pipeline? It seems like I need to pass in options for the Sprocket engine?

Was it helpful?

Solution

I found the solution here. It's all about monkey patching.

I prefer this variation, as it keeps the options already set for Haml::Template. Put this at the end of your haml initializer.

class Tilt::HamlTemplate
  def prepare
    options = @options.merge(:filename => eval_file, :line => line)
    # Use same options as Haml::Template
    options = options.merge Haml::Template.options
    @engine = ::Haml::Engine.new(data, options)
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top