Adding HAML to the Rails asset pipeline
Asked Answered
W

6

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?

Wellheeled answered 14/10, 2011 at 15:59 Comment(0)
A
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!

Anvil answered 12/9, 2012 at 12:59 Comment(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.Hoodlum
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.Hoodlum
M
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).

Meteoritics answered 10/6, 2013 at 9:50 Comment(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.Trimolecular
L
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:

Lepsy answered 14/3, 2012 at 5:19 Comment(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.Lodgment
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.Shivery
A
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...

Abiotic answered 1/2, 2012 at 13:58 Comment(0)
A
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
Abramson answered 16/9, 2013 at 20:5 Comment(0)
S
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
Siclari answered 13/8, 2019 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.