Converts HAML templates to ERB format.
Add this line to your application's Gemfile:
gem 'haml_to_erb'Or install directly:
gem install haml_to_erberb = HamlToErb.convert(haml_string)result = HamlToErb.convert_file("app/views/home.html.haml")
# => { path: "app/views/home.html.erb", errors: [] }
# With validation
result = HamlToErb.convert_file(path, validate: true)
# Delete original after conversion
result = HamlToErb.convert_file(path, delete_original: true)
# Dry run (preview without writing)
result = HamlToErb.convert_file(path, dry_run: true)
# => { path: "...", errors: [], dry_run: true, content: "<erb output>" }results = HamlToErb.convert_directory("app/views")
# => [{ path: "...", errors: [] }, ...]
results = HamlToErb.convert_directory("app/views",
delete_originals: true, # Remove .haml files
validate: true, # Validate ERB output
dry_run: true # Preview only
)# Convert all HAML files in directory
haml_to_erb app/views
# Convert a single file
haml_to_erb file.html.haml
# Validate output with Herb
haml_to_erb app/views --check
# Preview without writing files
haml_to_erb app/views --dry-run
# Delete originals after conversion
haml_to_erb app/views --delete
# Skip confirmation prompts
haml_to_erb app/views --delete --force
# Show full backtraces on errors
haml_to_erb app/views --debugFile operations return error details without raising exceptions:
result = HamlToErb.convert_file("nonexistent.haml")
# => { path: "nonexistent.erb", errors: [{ message: "File not found: nonexistent.haml" }], skipped: true }
# Permission errors
# => { ..., errors: [{ message: "Permission denied: ..." }], skipped: true }
# HAML syntax errors
# => { ..., errors: [{ message: "HAML syntax error: ...", line: 5 }], skipped: true }The converter uses Ruby's Prism parser to detect static attribute values (strings, symbols, numbers, booleans) and inline them directly into HTML. Dynamic expressions fall back to ERB tags.
%div{ class: "foo", data: { action: "click" }, href: @path }Becomes:
<div class="foo" data-action="click" href="<%= @path %>">Without this optimization, all values would be wrapped in ERB:
<div class="<%= "foo" %>" data-action="<%= "click" %>" href="<%= @path %>">This produces output that looks like what a human would write, making converted templates easier to review and maintain. It also avoids unnecessary ERB evaluation at runtime for values that never change.
- Double splat (
**) in attributes not supported (warning issued) - Whitespace removal markers (
>,<) parsed but whitespace not removed - Old doctypes (
!!! Strict,!!! Transitional) converted to HTML5 :markdownand other custom filters output as HTML comments
Requires the herb gem for ERB validation:
gem 'herb'result = HamlToErb.validate(erb_string)
result.valid? # => true/false
result.errors # => [{ message:, line:, column: }, ...]
# Or combine conversion + validation
result = HamlToErb.convert_and_validate(haml_string)
result.erb # => "<converted erb>"
result.valid? # => true/falseThe gem is available as open source under the terms of the MIT License.