ERB templates

Render a string

ERB mixes plain text with Ruby code inside special tags. Tags that start with an equals sign print a value into the output.

Call result with binding so the template sees local variables from the current scope.

require "erb" name = "Ada" puts ERB.new("Hello <%= name %>").result(binding)

ruby erb_string.rb

Hello Ada

Render a file

Real templates live in files, not in string literals. Read the file first and pass its content to ERB exactly as before.

Keep one template per file and give it an erb extension so editors highlight the mixed syntax.

require "erb" name = "Ada" File.write("hello.erb", "Hello <%= name %>\n") template = File.read("hello.erb") puts ERB.new(template).result(binding)

ruby erb_file.rb

Hello Ada

Trim mode

Loops and conditions often leave stray blank lines in the output. Trim mode with a dash removes the newline after a tag.

The dash variant is the default choice for list templates where every extra empty line shows.

require "erb" items = [1, 2, 3] template = "<% items.each do |n| %>- <%= n %>\n<% end %>" puts ERB.new(template, trim_mode: "-").result(binding)

ruby erb_trim.rb

- 1
- 2
- 3

A layout sketch

Pages often share one frame with changing content in the middle. Render the inner part first, then print it inside a layout template.

The content variable carries the inner HTML while the layout adds the shared heading around it.

require "erb" content = "Hello" layout = "<h1><%= content %></h1>" puts ERB.new(layout).result(binding)

ruby erb_layout.rb

<h1>Hello</h1>

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? require "erb" name = "Ada" puts ERB.new("Hello <%= name %>").result(binding)

    Answer

    Hello Ada

    The expression tag prints the value, and binding lets the template see the local variable.

  2. What does an ERB tag that starts with = do?

    Answer

    Prints the value of the expression into the output

    Tags with = print; tags without it only run code.

  3. What does this program print? require "erb" items = [1, 2, 3] template = "<% items.each do |n| %>- <%= n %>\n<% end %>" puts ERB.new(template, trim_mode: "-").result(binding)

    Answer

    - 1 / - 2 / - 3

    Trim mode removes the newlines that the code tags would otherwise leave between the lines.

  4. What does this program print? require "erb" content = "Hello" layout = "<h1><%= content %></h1>" puts ERB.new(layout).result(binding)

    Answer

    <h1>Hello</h1>

    The layout keeps its HTML around the value that the inner content supplies.

  5. Why is result(binding) passed instead of a plain result?

    Answer

    The template sees the local variables of the current scope

    binding is the scope the template evaluates in, which is how name and items are visible.

  6. What does this program print? require "erb" puts ERB.new("<% x = 5 %><%= x * 2 %>").result(binding)

    Answer

    10

    The first tag only assigns, and the second prints twice the value.

  7. What does trim mode "-" do?

    Answer

    Removes the newline after a tag, so loops leave no blank lines

    It is the default choice for list templates where every extra empty line shows.

  8. What does a tag written <% ... %> put into the output?

    Answer

    Nothing; it only runs the code

    Only the = form prints, which is why an assignment tag leaves the output alone.

  9. What does this program raise? require "erb" name = "Ada" puts ERB.new("<%= other %>").result(binding)

    Answer

    a NameError, because other is not defined

    binding carries the locals that exist, and other is not one of them.

  10. Why does the lesson keep each template in its own .erb file?

    Answer

    Editors highlight the mixed syntax, and one template lives in one place

    The render call is the same; the file is about keeping the template readable and editable.

Article author: Arthur Isaev