Slim vs Haml vs Erubi in 2026: The Ruby Template Engine Choice You Won’t Regret
Template engines are the part of a Ruby app nobody budgets for. You pick one in week one, it becomes load-bearing, and by the time you notice the costs — parse errors from a stray indentation, an unescaped interpolation in a partial, a rendering path that dominates your p95 — the migration is a quarter of work rather than an afternoon. The decision looks cosmetic and is not.
Three engines matter in 2026: Slim (5,372 stars, last push 2026-10-25), Haml (3,825 stars, 2026-11-03), and Erubi (396 stars, 2026-09-20) — the ERB implementation that Rails itself uses. They sit at different points on the abstraction curve: Slim and Haml replace HTML syntax with indentation and shorthand, while Erubi keeps ERB’s angle brackets and instead optimises the compiler underneath them. Tilt (1,939 stars, 2026-05-13) is the fourth name you will meet, and it is not a competitor at all — it is the adapter layer Sinatra and friends use to talk to all of them.
TL;DR
Choose Slim for new Rails or Sinatra applications where you control the markup: the syntax is compact, escaping is on by default, and it has been stable long enough that the ecosystem (editors, linters, slim-rails, slim_lint) is boring in the best way.
Choose Haml only if you already have Haml, or if your team writes markup by hand and values its explicit %tag syntax over Slim’s terseness. Haml 6 rewrote the parser for speed, but the ecosystem around it is thinner than it was a decade ago.
Choose Erubi when you want zero magic: it is ERB with a substantially better compiler, adds escaping without monkey patching, and is what Rails renders through anyway. If your templates are generated by another tool, or your team’s mental model is “HTML with holes in it”, Erubi wins on debuggability alone.
Use Tilt when you are writing a framework, a static-site generator, or a gem that must support whatever engine the user has installed.
The comparison table
| Dimension | Slim | Haml | Erubi |
|---|---|---|---|
| GitHub stars | 5,372 | 3,825 | 396 |
| Last push | 2026-10-25 | 2026-11-03 | 2026-09-20 |
| License | MIT | MIT | MIT |
| Syntax model | Indentation, tag shortcuts (div.card) | Indentation, explicit %tag.name | ERB: HTML with <% %> |
| Escaping default | On | On | Off unless escape: true |
| Rails integration | slim-rails | haml-rails | Built in (ActionView ERB handler) |
| Streaming support | Yes (Rails streaming) | Yes | Yes (line-oriented) |
| Relative compile speed | Comparable to ERB per project docs | Fast since the Haml 6 parser rewrite | Fastest escaping path; the compiler is the point |
| Dependency surface | Small, mature | Small | One file, under 150 lines |
| Best fit | New apps, compact views | Existing Haml codebases | Zero-magic views, generated templates, tools |
Decision matrix: pick in ten seconds
| Use case | Choose | Why |
|---|---|---|
| New Rails app, greenfield views | Slim | Compact markup, escaping on, mature tooling, no migration cost yet |
| Large legacy Haml codebase | Haml | Migration risk outweighs the syntax gains; Haml 6 is actively maintained |
| Server-rendered HTML you also edit by hand in a browser | Erubi | Output is plain HTML, so browser devtools and designers stay productive |
| Templates generated by code or another tool | Erubi | ERB is the lingua franca every Ruby tool can emit |
| Building a gem or framework that renders user templates | Tilt | One API, any engine the user installs |
| Highest-throughput rendering of simple partials | Erubi | Minimal runtime overhead and the fastest escaping implementation of the three |
| Views with heavy embedded SVG or inline JS | Erubi | Angle-bracket syntax avoids indentation fragility inside large literal blocks |
| Team of designers editing views | Slim, with linting | Short syntax plus slim_lint in CI keeps indentation honest |
Slim: minimal syntax, maximum discipline
Slim’s premise is that HTML is 60% punctuation you do not need. Tags become indentation, div with an id becomes #main, div with a class becomes .card, and closing tags disappear entirely.
| |
Two operators carry most of the semantics: = outputs an escaped expression, == outputs raw HTML. That second one is why Slim code reviews need attention — == on user-controlled data is an XSS hole, and it looks identical to = at a glance.
Configuration lives on the engine, and the option that matters most in production is pretty:
| |
In a Rails app, slim-rails wires this up so .slim views resolve automatically. In Sinatra, require "slim" is enough — Sinatra’s Tilt integration picks up the extension. The Slim project documents rendering speed as comparable to ERB/Erubis, which matches what you see in practice: the compile step is cached, so per-request cost is dominated by your own Ruby code inside the template, not by the engine.
Slim is also the engine where the linting story is strongest: slim_lint catches indentation problems, unbalanced tags, and questionable constructs before they reach a renderer. With indentation-sensitive syntax, that is not a nicety — it is the difference between a one-line fix and a stack trace pointing at a line that looks correct.
Haml: the original, still maintained
Haml is the engine that popularised indentation-based markup for Ruby, and its syntax is the most explicit of the three: every tag starts with %, which makes structure unambiguous at a glance.
| |
The trade-off is verbosity: Haml is roughly 20–30% longer than the equivalent Slim template because every tag carries its %. What you buy is unambiguity — you can read a deeply nested Haml file and know exactly which element you are looking at without counting indentation levels.
Haml 6 replaced the old parser with a faster one and dropped some historical baggage. That is good news for performance and bad news for anyone carrying custom helpers or third-party plugins from the Haml 4/5 era: audit your extensions before upgrading, because not every haml-contrib-style integration survived the rewrite. Filters (:javascript, :css, :markdown) still work and remain genuinely useful for embedding literal blocks without escaping gymnastics.
Configuration in Rails goes through an initializer:
| |
Haml is still released — the repository received commits in November 2026 — but it is maintained rather than evangelised. New projects pick Slim or ERB; existing projects stay. That is a rational equilibrium, not a red flag.
Erubi: ERB, with the compiler fixed
Erubi is the quiet winner of the last decade. It is a fork of Erubis that keeps ERB’s syntax and rewrites the internals: one file, under 150 lines of code, no monkey patching, an immutable design, and escaping that is dramatically faster than the original — the project documents 15x–6x faster escaping and roughly 81% smaller memory footprint compared with Erubis. Rails’ ActionView ERB handler is built on it, which means most Rails developers are already running Erubi without knowing the name.
| |
Note the escape: true option — ERB’s historical default is no escaping, and Erubi keeps compatibility while giving you a one-flag fix. The output above escapes Ada & Co automatically. There are also dedicated engine subclasses for the two patterns that trip people up: Erubi::CaptureBlockEngine and Erubi::CaptureEndEngine, which make <%= form_with do %> … <% end %> blocks behave the way Rails developers expect instead of emitting the block’s internals twice.
Erubi’s practical advantage is not benchmark bragging; it is that the template is HTML. Browser devtools map one-to-one onto the source. Designers can edit it. Copy-paste from a design mockup works. Diffing a partial against rendered output is trivial. For teams that have been burned by indentation-sensitive engines in large files, that is worth more than syntax savings.
The freeze: true option matters in modern codebases: it lets templates work under # frozen_string_literal: true and under Ruby’s --enable-frozen-string-literal, which several performance-minded shops enable globally. Slim has a similar story via its own options; Haml’s handling is less central to its design.
Where the engines bite in production
Whitespace bugs are parse errors, not layout bugs. In Slim and Haml, a mis-indented line usually fails to compile rather than producing wrong HTML — good for safety, expensive when a large file has mixed tabs and spaces. Fix it with an editor config and slim_lint/haml-lint in CI, not with review comments.
pretty mode is a production trap. Both Slim and Haml can emit nicely indented HTML. It costs CPU and bytes, and it changes whitespace inside <pre> and inline elements. Ship minified-by-default output and let your reverse proxy handle compression.
Escaping defaults differ, and the difference is security-relevant. Slim and Haml escape by default; ERB does not. If you move a template from Slim to Erubi, every <%= %> that renders user data is now unescaped until you add escape: true or wrap it in h(). Conversely, when porting to Slim, == is the raw-output operator and must be reviewed line by line.
Engine choice interacts with streaming. Rails streaming works with all three, but line-oriented Erubi templates flush more predictably than indentation-based engines that buffer nested blocks. If you stream long pages, measure time-to-first-byte rather than total render time.
Compilation caching hides template cost in development. In production, templates compile once and are cached in memory; the first request after deploy pays the cost. Benchmark with a warm cache or you will be optimising the compiler instead of your views.
Asset helpers are not the engine’s job. stylesheet_link_tag, javascript_include_tag, and importmap/Propshaft behaviour come from Rails, not from Slim or Haml. A migration that changes engines but keeps Rails views mostly works because the helper calls are identical — which is the strongest argument for keeping templates thin and pushing logic into helpers and presenters.
A minimal deployment for a Sinatra app using Slim looks like this — the engine is a gem, so the container build is the only operational concern:
| |
| |
If you are choosing an engine as part of a larger stack decision, the Ruby micro-framework comparison covers how Sinatra, Roda, and Grape wire rendering, and the Ruby ORM comparison explains the presenter patterns that keep templates thin. Our Ruby serialization guide is the natural companion when your views start rendering JSON instead of markup.
FAQ
Is Haml dying in 2026?
No, but it is no longer the default choice. The repository is actively maintained with commits in November 2026, and existing Haml codebases have no compelling reason to migrate. New projects tend to pick Slim for compact syntax or ERB/Erubi for familiarity, which leaves Haml with a stable, smaller user base rather than an abandoned one.
Is Slim actually faster than ERB?
The Slim project documents rendering speed comparable to ERB and Erubis, and that matches real-world measurements once template compilation is cached: the engine is rarely the bottleneck. Erubi’s advantage is specifically in the escaping path — its escaping is documented as 15x–6x faster than the original implementation, which shows up in templates that interpolate a lot of user data.
Which engine should a Sinatra app use?
Sinatra integrates through Tilt, so any of the three works with a single require. Slim is the most common pairing in tutorials and existing Sinatra code, Erubi is the lowest-magic option, and Tilt lets you mix engines per view if you inherit a codebase that already does.
Can I migrate from Haml to Slim incrementally?
Yes, and incremental is the only sane way. Both are indentation-based, so the translation is mostly mechanical (drop %, convert .class/#id shorthand), and the two can coexist in a Rails app as long as the appropriate gems are installed. Migrate leaf partials first, keep the layout and high-traffic pages for last, and run slim_lint over converted files before merging.
How do I prevent XSS with these engines?
Keep default escaping on (Slim and Haml do this out of the box), enable escape: true in Erubi, and treat every raw-output operator (== in Slim, != in Haml, <%== %> in ERB) as requiring a review. The dangerous case is not the engine — it is a developer reaching for raw output to “fix” double-escaped output without asking why it was escaped twice.
Do these engines work with generated or user-provided templates?
Only with care. All three compile template source into Ruby code, so rendering templates from untrusted sources is equivalent to running untrusted Ruby. For user-configurable layouts, prefer a restricted template language or a sandboxed renderer rather than handing Slim, Haml, or ERB arbitrary input.
Pick the engine that matches how your team reads markup, put a linter in CI, and keep logic out of the views. The engine is a small lever; template discipline is the big one.
💰 想测试你的市场判断力?我用 Polymarket 做预测市场交易——这是全球最大的预测市场平台,从大选结果到技术监管时间线,什么都可以押注。和赌博不同,这是真正的信息市场:你懂的信息越多,胜率越高。我靠预测技术相关事件的走向已经赚了不少。用我的邀请链接注册:Polymarket.com