Project

haml2html

0.0
The project is in a healthy, maintained state
A Haml-to-ERB migration tool for Rails templates.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 7.0, < 9.0
~> 5.0
~> 13.0
~> 0.10

Runtime

~> 6.3
 Project Readme

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 haml2html

Or 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.haml

The 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").render

Examples

Haml:

= form_with model: post do |form|
  = form.text_field :title

ERB:

<%= 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
    = menu

ERB:

<%= 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= status

ERB:

<%
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 :ruby filters.
  • 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.