What Is .erb
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 10, 2026
Key Facts
- ERB was included in Ruby's standard library starting with Ruby 1.4.0, released in 2000
- Ruby on Rails (launched 2004) adopted ERB as its default template engine, establishing it as the Rails standard
- ERB uses <%= %> to output values and <% %> to execute code without output, plus <%- -%> to suppress whitespace
- Approximately 70% of Rails applications use .erb templates as their primary view layer over Haml or Slim
- ERB automatically escapes HTML output by default in modern Rails versions, protecting against XSS injection attacks
Overview
.erb stands for Embedded Ruby and is a templating language that allows developers to embed Ruby code directly within text files, most commonly HTML documents. Originally included in Ruby's standard library and later adopted as the default template engine for Ruby on Rails in 2004, ERB remains one of the most popular templating systems in web development. ERB templates use the .erb file extension and enable the seamless integration of dynamic content with static HTML markup.
The power of ERB lies in its simplicity and deep integration with the Ruby language. Rather than requiring developers to learn a specialized templating syntax, ERB allows them to use pure Ruby code within specific delimiters to control how content is rendered. This approach means developers familiar with Ruby can immediately start writing templates without any additional learning curve. ERB templates are processed on the server side before being sent to the client's browser as plain HTML, which keeps application logic secure and separated from client-side code.
How It Works
ERB processes template files through a series of steps that transform embedded Ruby code into final HTML output. Here are the key mechanisms:
- ERB Delimiters (The Core Syntax): ERB uses specific delimiters to identify Ruby code within templates. The
<% %>delimiter executes Ruby code but produces no output, useful for loops and conditionals. The<%= %>delimiter executes code and displays the result directly in the HTML. Additional delimiters like<%- -%>suppress whitespace output, improving formatting of generated HTML. - Variable Substitution (Dynamic Content): Controllers pass instance variables (prefixed with @) to templates, allowing views to access data from application logic. For example,
<%= @user.name %>would display the current user's name wherever the expression appears. This separation of concerns keeps business logic in controllers and presentation logic in templates. - Control Structures (Conditionals and Loops): ERB supports full Ruby syntax including if/elsif/else statements and iterators like .each and .map. Templates can use
<% if condition %>...content...<% end %>to conditionally render sections, or<% @items.each do |item| %>to loop through collections and generate HTML for each element. - Method Calls (Template Helpers): Rails provides template helper methods that can be called from .erb files to generate links, forms, and other common HTML structures. Methods like
link_to,form_with, andrendermake building complex views more efficient than writing raw HTML manually. - Partial Templates (Code Reuse): ERB supports partial templates that can be included and reused across multiple pages. Using
<%= render 'shared/header' %>allows developers to extract common sections into separate files, reducing code duplication and improving maintainability across large applications.
Key Comparisons
ERB is one of several templating engines available for Rails developers. Here's how it compares to popular alternatives:
| Feature | ERB | Haml | Slim |
|---|---|---|---|
| Learning Curve | Low (pure Ruby syntax) | Medium (custom syntax required) | Medium (minimal syntax) |
| Performance Speed | Fast | Moderate (slower compilation) | Fast |
| Server Compatibility | Rails, Ruby, extensive | Rails, Ruby, limited | Rails, Ruby, growing |
| Market Share | ~70% of Rails applications | ~15% of Rails applications | ~10% of Rails applications |
| HTML Output Safety | Automatic escaping (Rails 4+) | Automatic escaping | Automatic escaping |
| Readability for Ruby Devs | Excellent (native Ruby) | Good (distinct syntax) | Good (minimal markup) |
Why It Matters
- Industry Standard Adoption: With the vast majority of Rails applications using ERB, learning this templating language is practically essential for Rails developers. Its ubiquity means that job postings, open-source projects, tutorials, and community support all heavily emphasize ERB expertise, making it the most practical choice for anyone entering Rails development.
- Built-in Security Protection: ERB includes automatic HTML escaping by default in modern Rails versions, protecting applications from common injection attacks and XSS vulnerabilities. Using
<%= %>automatically escapes user input, while raw HTML output requires explicit use of.html_safeorraw()methods, making security decisions explicit. - No Learning Curve for Ruby Developers: Because ERB uses native Ruby syntax, developers don't need to learn a new templating language syntax. This reduces context switching between application logic and view templates, allowing teams to leverage existing Ruby expertise directly in their templates without additional training.
- Deep Integration with Rails: ERB is deeply integrated into Rails conventions and workflows, functioning seamlessly with controllers, helper methods, partials, layouts, and the asset pipeline. The framework provides built-in methods and tag helpers that make common template tasks trivial to implement.
ERB remains the default choice for Rails template rendering because it provides an optimal balance of simplicity, power, and security. While other templating engines like Haml and Slim offer alternative syntax preferences and some stylistic advantages, ERB's combination of native Ruby syntax, widespread adoption across the Rails ecosystem, and comprehensive Rails integration makes it the most practical choice for the vast majority of Rails developers. Understanding ERB templating is fundamental to becoming proficient with Ruby on Rails web development.
More What Is in Daily Life
Also in Daily Life
More "What Is" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- Ruby on Rails Action View OverviewCC-BY-4.0
- Ruby Standard Library: ERB DocumentationRuby License
- Ruby on Rails Layouts and Rendering GuideCC-BY-4.0
Missing an answer?
Suggest a question and we'll generate an answer for it.