RubyGuides
Share this post!

Ruby Templating Engines: ERB, HAML & Slim

ERB is a templating engine.

A templating engine allows you to mix HTML & Ruby. This helps you generate your page dynamically using data from your database.

ERB is Rails default engine for rendering views.

To be precise, Rails uses an implementation called erubi instead of the ERB class from the Ruby standard library.

But what many people don’t know is that you can use ERB outside of Rails.

Now:

Let’s talk about the syntax of ERB & how it compares to other templating engines.

Embedded Ruby (ERB) Tutorial

An ERB template ends with a .html.erb or .erb extension.

A simple template looks like this:

Hello <%= name %>!

The <%= %> tag will be replaced by the templating engine by evaluating the 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.

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.

But if you want to use ERB outside of Rails

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 with books in your template for this to work.

Rails uses a metaprogramming trick called “variable binding”. That’s how it passes your controller instance variables into your template. But if you’re going to use ERB without Rails using a hash is a cleaner solution.

Comparing Templating Engines

Now:

Let’s compare the different templating engines.

You can access all of these engines using the Tilt gem. Tilt is an adapter gem like Faraday.

Example:

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 comparision @ the Github repository level:

REPO STARS LAST_COMMIT LATEST_RELEASE CREATED_DATE
jeremyevans/erubi 208 2018-10-23 21:55:00 2018-03-05 20:41:25 2016-11-10 22:42:44
haml/haml 3367 2018-10-23 01:19:36 2017-10-13 07:37:30 2008-02-11 22:55:26
slim-template/slim 4715 2018-09-03 00:15:35 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…

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.

Summary

You’ve learned about templating engines in Ruby!

Please share this article & subscribe to my newsletter if you haven’t yet (6500+ subscribers) so you can get more awesome content like this.

Thanks for reading 🙂

Leave a Comment: