Why We Chose Slim Templates Over ERB or HAML: A Deep Dive into Rails Blueprint's View Layer
August 16, 2025 • By Vladimir Elchinov
When building Rails Blueprint, one of our earliest architectural decisions was choosing the templating engine. While ERB comes built-in with Rails and HAML has a strong following, we chose SlimвАФand after thousands of lines of production code, we're confident it was the right choice.
This article explores why Slim templates power Rails Blueprint, backed by real performance data, developer productivity metrics, and practical examples from our codebase.
The Problem with ERB
ERB (Embedded Ruby) is Rails' default templating engine, and for good reasonвАФit's straightforward, requires no additional dependencies, and every Rails developer knows it. However, after maintaining large Rails applications, we identified several pain points:
The <% and %> tags create visual clutter, making it harder to scan the template structure at a glance.
2. Closing Tag Maintenance
Every opening HTML tag needs a corresponding closing tag. In deeply nested structures (common in admin panels), this becomes error-prone:
</div>
</div>
</div>
</div>
</div> <!-- Which div does this close? -->
lang-erb
3. Character Count Overhead
Our analysis of Rails Blueprint's admin panel showed that ERB templates would be approximately 35% larger in character count compared to their Slim equivalentsвАФthat's more scrolling, more typing, and more potential for errors.
Why Not HAML?
HAML addresses many of ERB's verbosity issues with its indentation-based syntax:
HAML is good and actually performs slightly better than Slim in benchmarks, but we chose Slim for these reasons:
The % Symbol Tax: Every HTML element needs a % prefix, adding visual noise
Developer Preference: Our team found Slim's syntax cleaner and more intuitive
Attribute Syntax: Slim's square bracket notation for attributes is more flexible
Community Momentum: Slim has stronger adoption in modern Rails applications
Enter Slim: The Best of Both Worlds
Here's the same template in Slim:
.container
.row
.col-md-8
- if @posts.any?
- @posts.each do |post|
.post-card
h2 = link_to post.title, post_path(post)
p.meta = post.published_at.strftime("%B %d, %Y")
.content = truncate(post.content, length: 200)
- else
p No posts found.
lang-slim
Notice the improvements:
No % symbols for HTML elements
No closing tags
Clean separation between HTML structure and Ruby code
Minimal syntax noise
Performance Benchmarks
We ran comprehensive benchmarks comparing all three templating engines using Rails Blueprint's actual template patterns:
Rendering Speed (10,000 iterations)
# Test setup: Rendering a typical Rails Blueprint post card component
n = 10000
Benchmark.realtime { n.times { render_template } }
# Results:
# HAML: 307,427 renders/sec
# Slim: 269,222 renders/sec
# ERB: 41,370 renders/sec
# Performance comparison:
# Slim is 550% faster than ERB
# HAML is 12% faster than Slim
# Both Slim and HAML are 5-6x faster than ERB
lang-ruby
While HAML edges out Slim slightly in raw performance, both are dramatically faster than ERB. The performance difference between Slim and HAML is marginal in real-world applications.
Memory Usage
# Memory allocation per template render (average over 100 renders)
HAML: 2,225 bytes
Slim: 2,712 bytes
ERB: 2,944 bytes
lang-ruby
All three templating engines have similar memory footprints, with HAML being the most memory-efficient. The differences are marginal in real-world applications.
Template File Size Comparison
Rails Blueprint's admin panel contains 59 Slim view files totaling 425 KB. When we convert sample templates to ERB for comparison:
That's a 42% reduction in file size compared to ERBвАФless code to maintain, review, and debug. Across an entire application, this translates to significantly smaller codebases.
Developer Productivity Gains
1. Faster Template Writing
Writing templates in Slim is noticeably faster due to less typing and cleaner syntax:
With no closing tags and minimal syntax, developers can write templates faster and with fewer keystrokes.
2. Fewer Syntax Errors
The most common template errors by type:
ERB: Unclosed tags ( missing)
HAML: Indentation inconsistencies
Slim: Indentation inconsistencies
Slim and HAML eliminate the unclosed tag problem entirely, leading to fewer errors overall.
3. Code Review Efficiency
Cleaner syntax makes code reviews more efficientвАФreviewers can focus on logic and structure rather than parsing through HTML noise and matching closing tags.
Real-World Examples from Rails Blueprint
1. Component Architecture
Our actual search component from Rails Blueprint showcases Slim's clean syntax:
For teams considering the switch, here's our recommended approach:
1. Start with New Features
Don't convert everything at once. Start using Slim for new views:
# config/application.rb
config.generators do |g|
g.template_engine :slim
end
lang-ruby
2. Use Our Converter Tool
We've built a free online converter at railsblueprint.com/html2slim that handles ERB to Slim conversion. While automated conversion isn't perfect, it provides a solid starting point that you can refine.
3. Convert High-Traffic Templates First
Focus on templates that are edited frequently or have performance implications:
Forms and form partials
Index pages with complex tables
Shared partials used across the application
4. Team Training
The learning curve is minimal. Key points for developers:
Indentation matters (like Python)
No closing tags needed
Use - for Ruby code without output
Use = for Ruby code with output
Attributes go in [] or use inline style
Common Pitfalls and Solutions
1. Indentation Errors
/ Wrong - will cause an error
.container
.row / This should be indented
/ Correct
.container
.row
lang-slim
2. Multiline Attributes
/ For long attribute lists, use square brackets
input[
type="text"
name="user[email]"
class="form-control form-control-lg"
placeholder="Enter your email"
required=true
data-validate="email"
]
lang-slim
3. Working with Translations
/ Clean integration with Rails i18n
.welcome-message
h2 = t('dashboard.welcome', name: current_user.name)
p = t('dashboard.notifications', count: @notifications.count)
/ HTML-safe translations (automatically safe when key ends with _html)
.alert.alert-info
= t('dashboard.announcement_html', link: link_to(t('common.learn_more'), docs_path))
lang-slim
Performance Tips for Slim Templates
1. Avoid Unnecessary String Interpolation
/ Slower
div class="post-#{@post.status} post-#{@post.category}"
/ Faster
div class=post_classes(@post)
lang-slim
2. Use Rails Collection Rendering
/ Instead of manual iteration
- @posts.each do |post|
= render 'post_card', post: post
/ Use Rails collection rendering (faster and cleaner)
= render partial: 'post_card', collection: @posts, as: :post
/ With caching
= render partial: 'post_card', collection: @posts, as: :post, cached: true
lang-slim
3. Leverage Slim's Built-in Optimizations
Slim automatically optimizes certain patterns:
Static text is pre-compiled
Attribute merging is optimized
Unnecessary whitespace is removed in production
The Rails Blueprint Advantage
By choosing Slim for Rails Blueprint, we've achieved:
42% smaller templates - Dramatically less code than ERB
5.5x faster rendering - Both Slim and HAML vastly outperform ERB
Cleaner syntax - No % symbols (unlike HAML), no closing tags (unlike ERB)
99 templates in 475KB - Entire view layer in under half a megabyte
Better developer experience - Minimal syntax noise, maximum clarity
Conclusion
Slim templates have been instrumental in making Rails Blueprint's codebase clean, maintainable, and performant. The initial learning curve (usually just a few hours) pays dividends in long-term productivity and code quality.
For Rails developers looking to improve their view layer, Slim offers the perfect balance of simplicity, performance, and expressiveness. It's not just about writing less codeвАФit's about writing better code.
Whether you're starting a new Rails project with Rails Blueprint or considering a migration from ERB, Slim templates offer compelling advantages that go beyond mere syntax preferences. The numbers speak for themselves: faster development, fewer errors, and better performance.
Resources
Rails Blueprint - Production-ready Rails starter template with Slim
Rails Blueprint uses Slim templates throughout its codebase, from the admin panel to public-facing pages. Experience the difference in your next Rails project.