ERB is a templating engine.
A templating engine allows you to mix HTML & Ruby so you can generate web pages using data from your database.
ERB is Rails default engine for rendering views.
Note: Rails uses an implementation called erubi instead of the
ERBclass from the Ruby standard library.
As you’ll learn later in this article, you don’t need Rails to use ERB.
An introduction to ERB’s syntax, how to use ERB in your Ruby projects & how it compares to other templating engines, like HAML.
Let’s do this!
An ERB template is a file that ends with an
Everything you write inside an ERB template is HTML.
Unless… you use a special tag!
Here’s an example:
Hello <%= name %>!
<%= %> tag will be replaced by the templating engine by evaluating the Ruby code inside it.
It’s like string interpolation!
Notice the equals sign in
That tells ERB to render the contents of this tag.
If you want to write a loop or an if statement in ERB you want to leave out the equals sign so ERB doesn’t render things that you don’t need.
You may even get an error if you don’t use the correct ERB tag.
Here’s an example:
<% if @favorite_food == "chocolate" %> Are you a chocolate lover? Here are some of our best PREMIUM chocolate bars! <% else %> Here are our top 10 snacks that people bought this month. <% end %>
This is a ERB if statement, like a regular Ruby if statement but wrapped around the special templating syntax so it can be evaluated & replaced by the output when this page is rendered for the user.
Here’s an ERB loop:
<% @books.each do |book| %> <%= book.title %> <%= book.author %>
<% end %>
Given an array of books, this will print every book with a line break between them.
If you’re using Rails, it will render this template for you when you call the associated controller action.
If you want to use ERB outside of Rails to build reports & static websites…
You can do this:
require 'erb' Book = Struct.new(:title, :author) template = ERB.new(File.read('template.erb')) template.result_with_hash(books: [Book.new("test"), Book.new("abc")])
You’ll need to replace
books in your template for this to work.
Rails uses a metaprogramming trick called “variable binding”.
That’s how Rails shares instance variables from your controller with your template. But if you’re going to use ERB without Rails using a hash (with
result_with_hash method) is a cleaner solution.
What do you think?
Let’s compare the different templating engines.
require 'tilt' Tilt::ErubiTemplate.new("test.erb").render require 'tilt/haml' Tilt::HamlTemplate.new("test.haml").render require 'slim' Slim::Template.new("test.slim").render
Here’s a benchmark between the different implementations:
Comparison: erubi: 5786.0 i/s ERB: 4438.8 i/s - 1.30x slower Haml: 1340.0 i/s - 4.32x slower Slim: 724.1 i/s - 7.99x slower
Here’s what HAML syntax looks like:
%head %title Ruby Templating Engines = 123 * 2 %body - foo.each do |bar| %p This Is Cool
It’s different from plain HTML, so you’ll need to get used to it.
Even more succinct is Slim’s syntax:
head title Ruby Templating Engines = 123 * 2 body - foo.each do |bar| p | This Is Cool
Slim uses the pipe character for multi-line blocks of content, in Haml you don’t need that.
Let’s take a look at a comparison @ the Github repository level:
|jeremyevans/erubi||255||2019-11-05 14:09:06||2019-09-25 15:44:36||2016-11-10 22:42:44|
|haml/haml||3461||2019-12-27 10:51:59||2019-08-06 12:01:09||2008-02-11 22:55:26|
|slim-template/slim||4898||2019-12-15 23:55:23||2018-09-02 23:54:10||2010-09-13 01:32:07|
So which one should you use?
I think it’s mostly personal preference.
But one thing is clear:
Choose one for a specific project & write all your templates using that engine.
Don’t mix & match.
I like to stick with ERB templates…
With the right editor snippets & support it’s not that much extra writing.
You even have converters available (from/to ERB) if you ever want to move to another engine.
You’ve learned about templating engines in Ruby!
Please share this article & subscribe to my newsletter if you haven’t yet (9000+ subscribers) so you can get more awesome content like this.
Thanks for reading 🙂