13

I would like to serve client side templates that have been pre-processed through HAML. I have tried using the haml_assets gem and adding the following code to an initializer:

Rails.application.assets.register_engine ".haml", Tilt::HamlTemplate

Both of these methods serve the raw HAML and not compiled HAML when I access the asset. How can I add HAML to the pipeline?

0

6 Answers 6

11

Just to clear things up, since I find the current answers a bit irritating (led me to the right direction though)

It works, if i have this line in an initializer file:

# config/initializers/haml_assets.rb
Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate

Throw your haml files into the assets folder, for example:

# app/assets/templates   

Do not use the haml_asset gem though!

2
  • This doesn't work for me on Rails 3.2.13. Edit: Maybe it does work and I just needed to change the template to force a recompile? Same thing was required for user2470396's answer.
    – Ibrahim
    Sep 12, 2013 at 1:56
  • Yeah, after I modified the file it worked, though I tried the application.rb modification after this one didn't work, but I then reverted everything from the other answer and it's still working.
    – Ibrahim
    Sep 12, 2013 at 2:01
10

The following code in application.rb works for me in Rails 3.2 (both in development and in production after pre-compilation):

require 'haml'

config.assets.paths << Rails.root.join("app", "assets", "templates")

class HamlTemplate < Tilt::HamlTemplate
  def prepare
    @options = @options.merge :format => :html5
    super
  end
end

config.before_initialize do |app|
  require 'sprockets'
  Sprockets::Engines #force autoloading
  Sprockets.register_engine '.haml', HamlTemplate
end

This allows you to put you templates in app/assets/templates named with the suffix .html.haml (you need the .html in there or else .htm files get generated instead of .html in the pre-compilation process).

1
  • 1
    Worked for me too (in dev so far, at least). One note: If your HAML template is still rendering as raw HAML, make a small change and reload. Rails seems pretty aggressive about caching the assets, even in dev.
    – Micah
    Jul 7, 2013 at 13:54
5

This works for me:

# app/assets/javascripts/test.html.haml

%p hello

# config/initializers/haml_template.rb

Rails.application.assets.register_mime_type 'text/html', '.html'
Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate

This works for http://127.0.0.1:3000/assets/test.html.haml

Rails.application.assets is a Sprockets::Environment.

See here for API reference:

2
  • This works dandy for me in my dev environment, but when I push it to production I'm still getting raw HAML. Digging in right now but if anyone has a clue, I'd be much obliged. May 21, 2013 at 22:04
  • It is not working in my dev environment, can you give me help? If I access the .html.haml or the .html file directly it serves as a string, not as html, any of them. Jul 20, 2013 at 2:18
0

Using the same approach I've got:

%tr
  %th
    %a.action.link.show
  %td
  %td

returned as pure haml. But

%tr
  %th
    %a.action.link.show
  %td cell 2
  %td cell 3

was served as html chunk. So I think this is something with haml gem. You can force haml conversion with something like this:

%tr
  %th
    %a.action.link.show
  %td &nbsp
  %td &nbsp

Hope it helps...

0

Two of the previous answers here had to be combined before we had a full solution.

The following line works in development:

# config/initializers/haml_assets.rb
Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate

But then fails on any precompile.

For asset-served haml to work after a precompile, we also needed these lines in application.rb:

require 'haml'

config.assets.paths << Rails.root.join("app", "assets", "templates")

class HamlTemplate < Tilt::HamlTemplate
  def prepare
    @options = @options.merge :format => :html5
    super
  end
end

config.before_initialize do |app|
  require 'sprockets'
  Sprockets::Engines #force autoloading
  Sprockets.register_engine '.haml', HamlTemplate
end
0

With sprockets 3 and 4

# in /config/initializers/haml.rb
Rails.application.config.assets.configure do |env|
 env.register_mime_type "text/haml", extensions: %w(.haml .html.haml)
 env.register_transformer 'text/haml', Tilt::HamlTemplate.default_mime_type, Tilt::HamlTemplate
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.