erb, haml or slim: which one do you suggest? And why?

Ruby on-RailsHamlErbSlim Lang

Ruby on-Rails Problem Overview


I am learning Rails and I have seen these template engines. I have no experience with them (only erb).

But as I am a beginner, I am really confused. Which one do you suggest and why? Erb, Haml or Slim? Please tell your reason for preferring one over the others. And if you have any other recommendations, please let us know.

EDIT: I am NOT looking for a winner here. I just want to hear your opinions about them, their syntax, speed of execution, and so on.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Two big advantages of using slim over haml:

  1. Slim is currently about eight times faster than haml.

  2. Slim supports HTTP streaming, while HAML doesn't.

  3. Slim has a more natural syntax: a href="foo.html"

Solution 2 - Ruby on-Rails

ERB is good mainly if you have a web designer that will work on plain HTML and does not know either haml or slim. This way they can write HTML and you can embed ruby logic with the proper tags.

If you work on both HTML and ruby logic, or your designer is ready to learn something new (like HAML) I'd go for HAML. It is a lot more ruby-friendly, reduces char count by much and a lot more readable than ERB.

For example (taken from official HAML site):

In ERB your view will look like this:

<div id="profile">
  <div class="left column">
    <div id="date"><%= print_date %></div>
    <div id="address"><%= current_user.address %></div>
  </div>
  <div class="right column">
    <div id="email"><%= current_user.email %></div>
    <div id="bio"><%= current_user.bio %></div>
  </div>
</div>

While in HAML it will look like this:

#profile
  .left.column
    #date= print_date
    #address= current_user.address
  .right.column
    #email= current_user.email
    #bio= current_user.bio

A lot cleaner!

As for the difference between HAML and SLIM - I never really worked with SLIM but I guess it is a matter of taste - take a look at both syntaxes and decide which looks better in your eyes. I don't think there is a definite winner between those two (HAML/SLIM).

Solution 3 - Ruby on-Rails

Off the top of my head this is what I came up with

ERB:

Pros

  • default out of the box
  • not white space dependent
  • lowest barrier of entry (if coming from HTML) as its HTML with Ruby code sprinkled in
  • most IDE's lexers read it by default
  • DHH prefers it
  • legacy apps are probably still using it

Cons

  • more verbose
  • content_for tags in helpers and views can get out of hand quickly
  • content_for tags makes nesting tags harder as erb only returns the last line in the block. so you have to append to a string and then return that.

HAML

Pros

  • more concise. no closing tags, fits in smaller screens
  • visually cleaner structure
  • has built in helpers (haml_concat, haml_capture) to utilize haml in helper methods
  • class chaining
  • lots of useful syntactic sugar like # for divs or . for class chaining, or :javascript for JS tags

Cons

  • whitespace dependent which makes for some hard errors to figure out at times
  • complex tags usually need to resort to "hash" format. (Although I actually think this is a great example of flexibility to someone starting out it could be a pain.)
  • added as a gem (again probably a stretch to put this as a con)
  • designers might have some trouble adjusting
  • in addition to the general whitespace warning... simple whitespace errors eg. tabs and spaces for indentation, can cause pages to err in production which normal specs/test won't catch. Moral: Expect greater need for view tests and possibly don't use haml for mission critical views, unless you're sure that your tests are testing the actual rendering of the view.
  • is slower (than erb)
    • caveat: this is ruby code we're talking about if speed is a blocking issue in your application there are alternatives to ruby, e.g. haskell

Solution 4 - Ruby on-Rails

The question for me comes down to would you rather put % before every tag or | before every new block of text?

Slim:

 tag(attr= "value")
  | text

Haml:

 %tag{attr: "value"}
   text

One more thing to lookout for: haml assumes a white space between new lines (remove whitespace in haml) while slim assumes no space (Add whitespace in Slim here and here)

Solution 5 - Ruby on-Rails

https://github.com/scalp42/hamlerbslim - is an independent benchmark which shows Slim and Erb as winners, performance wise (slim tends to reduce the HTML output size too.)

My personal opinion is that overall, Slim and Haml will save you time (== money) in terms of maintenance, providing you have Haml/Slim savvy people looking after your views.

If you don't have those people, Erb is definitely the way to go, because despite the best will in the world, there are a lot of very inexpensive people available who can work with HTML/Erb, but find Haml/Slim a complete mystery.

Best of all cases, train these people to use Slim or at least expose them to it, and keep the numbers of the ones who "get it."

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOmid KamangarView Question on Stackoverflow
Solution 1 - Ruby on-RailsBart ten BrinkeView Answer on Stackoverflow
Solution 2 - Ruby on-RailsErez RabihView Answer on Stackoverflow
Solution 3 - Ruby on-RailsengineerDaveView Answer on Stackoverflow
Solution 4 - Ruby on-RailsmontrealmikeView Answer on Stackoverflow
Solution 5 - Ruby on-RailsocodoView Answer on Stackoverflow