§2023-02-28


ERB is part of the Ruby standard library. You do not need to install any other software to use it. Rails uses an improved version, called Erubis. ri ERB to read the docs

<html>
  <body>
    <h1>Messages for <%= name %></h1>
    <ul>
      <% messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  </body>
</html>
puts name

messages.each do |message|
  puts message
end

ERB, when executed, does exactly this, except that = as part of the ERB tag <%= ... %> will not output things to the terminal, but capture it, and insert it to the surounding text (HTML code, in our case) in place of this tag. Ruby code in ERB tags that do not have an equal sign, such as <% ... %> will be executed, but any return values won’t be captured, and just discarded.


ERB recognizes certain tags in the provided template and converts them based on the rules below: