haml2html
haml2html converts Haml templates to Rails ERB templates. It is built for Rails app migrations where rendered HTML equivalence matters more than preserving original source formatting.
Install
gem install haml2htmlOr in a Gemfile:
gem "haml2html"CLI
haml2html app/views/posts/show.html.haml app/views/posts/show.html.erb
haml2html --stdin < app/views/posts/show.html.hamlThe CLI writes to stdout unless an output path is provided. Unsupported syntax is reported with file and line diagnostics and exits nonzero.
Ruby API
require "haml2html"
erb = Haml2html::Converter.new("%p= post.title\n", filename: "show.html.haml").renderExamples
Haml:
= form_with model: post do |form|
= form.text_field :titleERB:
<%= form_with model: post do |form| %>
<%= form.text_field :title %>
<% end %>Haml:
= render(SplitDropdownComponent.new) do |component|
- component.with_button do
= render(Button::IconComponent.new(...))
- component.with_menu do
= menuERB:
<%= render(SplitDropdownComponent.new) do |component| %>
<% component.with_button do %>
<%= render(Button::IconComponent.new(...)) %>
<% end %>
<% component.with_menu do %>
<%= menu %>
<% end %>
<% end %>Haml:
:ruby
if published
status = "Published"
else
status = "Draft"
end
%p= statusERB:
<%
if published
status = "Published"
else
status = "Draft"
end
%>
<p><%= status %></p>Supported
- Haml tags, nesting, static attributes, text, interpolation.
- Ruby output and control flow:
=,!=,- if,- each do, and similar blocks. - Yield-based Ruby block DSLs, such as ViewComponent slots and Rails form builders.
- Public comments and silent comments.
-
:plain,:escaped,:javascript,:css,:erb, and:rubyfilters. - Literal dynamic Haml attribute hashes, including simple
class,data,aria, and boolean attributes.
Limitations
This is a migration tool, not a full source-preserving formatter. Output whitespace and quote style may differ from Haml output, while rendered HTML should remain equivalent for supported constructs. Unsupported filters or nodes fail with diagnostics instead of emitting known-wrong ERB.
Object references such as %div[user] are not converted yet.