Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force usage of hamlit in Rails when haml gem is also bundled? #30

Closed
bensheldon opened this issue Jun 24, 2015 · 5 comments
Closed

Force usage of hamlit in Rails when haml gem is also bundled? #30

bensheldon opened this issue Jun 24, 2015 · 5 comments

Comments

@bensheldon
Copy link

I'm in the situation where I am trying to use hamlit in my Rails application, but unfortunately I have a Gemfile dependency on rails_admin, which has a dependency on the original haml gem. With both haml and hamlit bundled, it seems like the original haml gem wins out. Is there a way that I can force Rails to use hamlit and not haml when rendering templates?

Thanks for the help.

@k0kubun
Copy link
Owner

k0kubun commented Jun 24, 2015

Thank you for reporting an issue. I could reproduce your environment and resolve it.
Could you try to put following code in config/initializers/hamlit.rb?

# config/initializers/hamlit.rb
module Hamlit
  class TemplateHandler
    def call(template)
      Engine.new(
        generator: Temple::Generators::RailsOutputBuffer,
        filename: template.identifier,
      ).call(template.source)
    end
  end
end

ActionView::Template.register_template_handler(
  :haml,
  Hamlit::TemplateHandler.new,
)

I think Hamlit should overwrite Haml by default. I'll fix it later.

@bensheldon
Copy link
Author

Thanks for the quick help! In exploring the problem further, I think I may have been mistaken about hamlit not being mounted in the presence of haml in bundle. In doing further profiling, it seems like haml monkeypatches in ActionView#render_with_haml which gets called even if the template itself is being rendered by hamlit. This was confusing me. Here is a portion of my callstack:

...
… actionview-4.2.2/lib/action_view/renderer/renderer.rb:46:in `ActionView::Renderer#render_partial'   (147 samples - 48.36%)
… actionview-4.2.2/lib/action_view/renderer/renderer.rb:19:in `ActionView::Renderer#render'   (267 samples - 87.83%)
… actionview-4.2.2/lib/action_view/helpers/rendering_helper.rb:26:in `ActionView::Helpers::RenderingHelper#render'    (263 samples - 86.51%)
… haml-4.0.6/lib/haml/helpers/action_view_mods.rb:3:in `ActionView::Base#render_with_haml'    (263 samples - 86.51%)
/Users/bensheldon/Workspace/dayoftheshirt-rails/app/views/shirts/index.html.haml::in `ActionView::CompiledTemplates#_app_views_shirts_index_html_haml___1478955222455656801_2252899580' (124 samples - 40.79%)
… actionview-4.2.2/lib/action_view/template.rb:143:in `block in ActionView::Template#render'  (264 samples - 86.84%)
...

But when looking at what is actually rendering the template, it was hamlit (I did this by looking at where the most memory allocations were, not sure if there is a better way to introspect)... even without the snippets you provided. So I think there was never a problem in the first place in regards to rendering (but for some reason haml feels it necessary to monkeypatch in that method).

Also, the snippet you pasted I had a typo I think. There is not a template option Hamlit::Engine. This is snippet that worked for me (though ended up being unnecessary).

# config/initializers/hamlit.rb
module Hamlit
  class TemplateHandler
    def call(template)
      Engine.new(
        generator: Temple::Generators::RailsOutputBuffer,
        attr_quote: '"',
      ).call(template.source)
    end
  end
end

ActionView::Template.register_template_handler(
  :haml,
  Hamlit::TemplateHandler.new,
)

Thank you again!

Edit: Also wanted to add that I'm seeing 20% faster page loads and 20% less memory usage in switching to Hamlit. Which is awesome!

@k0kubun
Copy link
Owner

k0kubun commented Jun 25, 2015

not sure if there is a better way to introspect

Hamlit test cases generate incompatibilities against Haml. You can check which template engine is used with them. https://github.com/k0kubun/hamlit/tree/39b4634a3e1e3de529cda01c380c33b9fa54378f/doc/haml

Also, the snippet you pasted I had a typo I think. There is not a template option Hamlit::Engine.

🙇

Thank you again!
Edit: Also wanted to add that I'm seeing 20% faster page loads and 20% less memory usage in switching to Hamlit. Which is awesome!

Thank you for using hamlit. :)

@k0kubun k0kubun closed this as completed Jun 25, 2015
@connorshea
Copy link
Contributor

I'm running into this same issue, how can I tell for sure that Hamlit is being used and not Haml?

@k0kubun
Copy link
Owner

k0kubun commented Apr 12, 2016

Didn't #30 (comment) work for you?

If it didn't, please try to put require 'haml' before Bundler.require(*Rails.groups) in config/application.rb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants