ERB is a simple templating system for Ruby, embedding code in any plain-text document. It is often used for HTML generation in web frameworks (such as Ruby on Rails).
ERB stands for Embedded Ruby and is a markup-based way of embedding Ruby code into text files.
The erb tag may be used for the standard (pure-ruby) ERB library, the C-based implementation eRuby or the even-faster Erubis library. ERB ships with Ruby installations as part of the Standard Library; the documentation for ERB is available on ruby-doc.org.
ERB is commonly used as templating system, for example in producing HTML pages with dynamic content (such as in the Ruby on Rails web framework) but is also useful in a variety of other contexts, such as code generation, mass emailing, or simple text reporting.
ERB markup is generally seen as <% ... %>
and <%= ... %>
. See the section "Recognized Tags" in the documentation for the ERB class for the full set of markup.
A simple example:
template.erb
Hello, <%=name%>. Are you:
<% moods.each_with_index do |mood,i|%>
<%=i%>. <%=mood%>
<% end %>
code.rb
require 'erb'
template = ERB.new(IO.read('template.erb'))
name = "Phrogz"
moods = %w[ Happy Angry Sad ]
message = template.result(binding)
puts message
#=> Hello, Phrogz. Are you:
#=>
#=> 0. Happy
#=> 1. Angry
#=> 2. Sad