I have a rails project that previously was written using ERB views. I'm slowly converting the project to HAML through a process of creating a duplicate for each view as I start on it, building the HAML view to parity (with changes based on a new UI framework), and then deleting the old ERB view. The problem with this process is that rails favors the ERB view files, so I have to either rename the old view file or wait until I delete it to see the new one rendered.
While I don't consider it good practice to ship the code to production with duplicate view files for the same rendering format, this got me thinking about the process of choosing a view file: Is there a priority defined globally somewhere I could change to prefer HAML files if present in a project? How does rails choose which view file to render from?
Handlers are prioritized in the order they are registered. You can re-register a handler so it's prioritized last. For example to move ERB to last priority:
ActionView::Template.unregister_template_handler(:erb)
ActionView::Template.register_template_handler(
:erb, ActionView::Template::Handlers::ERB.new)
To do the same with haml:
ActionView::Template.unregister_template_handler(:haml)
ActionView::Template.register_template_handler(
:haml, Haml::RailsTemplate.new)
More generally, if you search through a gem for register_template_handler
, then you'll likely find the handler you need to register.
Tested in Rails 7.0