ERB Cribsheet

Facts about ERB
  • “Embedded Ruby”
  • One of a class of ‘template languages’
  • There are many
  • They do a simple job (used to be called “mail merge”)

What it does

  • Erb file without any <% %> blocks remains unchanged
  • <% %> blocks must contain legal ruby code!
  • The code between the angle brackets is evaluated.
  • And only in the case of <%= %> it’s inserted into the resulting text

Example



<% %w(Jones Hickey Salas Madeiras).each do |name| %>
  ----
  Dear Mr. <%= name %>,
  I am writing to let you know that I have moved, and my new
  address is 1 Main Street, San Fransisco, CA. Please make
  a note of it.

  Sincerely,

  Tom
  -----
<% end %>