106

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.

Yu Hao
  • 111,229
  • 40
  • 211
  • 267
Omid Kamangar
  • 5,580
  • 9
  • 35
  • 67
  • 7
    The short answer is, as a beginner, use ERB. – Scott Schulthess Aug 30 '13 at 19:04
  • http://sephinrothcn.wordpress.com/2014/04/14/slim-vs-haml-performance-perspective/ – Virtual Nov 07 '14 at 11:40
  • Although not a template engine, you may want to look into the [dom gem](https://rubygems.org/gems/dom) that I have developed. It allows you to seemlessly write HTML codes as Ruby. – sawa May 05 '16 at 02:54
  • 15
    This "not constructive" question was incredibly helpful for me. Thanks for asking it, even if the mods for whatever reason don't feel that it fits. It was one of the top google hits and the many answers here helped me make my decision. – Andy Baird Oct 26 '16 at 20:22
  • 2
    it still is the #1 google result for 'rails html erb' – Orwellophile Nov 24 '16 at 00:05
  • I work almost exclusively in HAML and I can't recommend it. The examples on the HAML site may be prettier but in real life you'll find lots of situations where it is uglier and confusing. There is definitely more to learn, and less flexibility. The fact that its syntax mixes an outline style with a nesting style (for attributes) breaks the outline paradigm when you have a lot of attributes. – iconoclast May 09 '20 at 22:52

5 Answers5

88

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"

Mohamad
  • 32,727
  • 31
  • 131
  • 208
Bart ten Brinke
  • 1,046
  • 7
  • 5
  • 3
    Do you have a reliable source for your statement about the speed of Slim vs Haml? I read a lot about this everywhere, but except on [Slim's GitHub page](https://github.com/stonean/slim) I don't find much provable information about it. – Joshua Muheim Aug 23 '12 at 08:31
  • 4
    @JoshuaMuheim Slim comes with benchmark code that you can modify/test on your own machine: https://github.com/stonean/slim#testing – Gerry Aug 28 '12 at 21:39
  • 6
    +1 Slim supports HTTP streaming, we are experiencing problems with our payment Gateway and Heroku, it seems that streaming HTTP is a way to solve this issue but as our application runs with HAML this solution is not an option anymore – Flov Sep 14 '12 at 02:35
  • 1
    @DamianNowak I think your statement is over-generalized. You probably mean speed should not necessarily be deciding factor, given other factors. Also, what if a library was slow enough to become a bottleneck? I'm sure devs would take notice then. Devs do have to give *some* weight to speed, or they wouldn't be very smart, eh? – Kelvin Apr 10 '13 at 20:01
  • 16
    Premature optimization isn't a good idea if it requires a lot of additional work, but simply choosing one similar library over another because of a significant performance benefit is not premature. – Jamon Holmgren Apr 14 '13 at 00:10
  • For existing project using HAML, they can switch to [Faml](https://rubygems.org/gems/faml) or [Hamlit](https://rubygems.org/gems/hamlit). For new project maybe Slim is better due to popularity (see [popularity comparison](https://www.ruby-toolbox.com/compare/faml,hamlit,slim)). – Franklin Yu May 23 '19 at 02:29
68

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 he 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 you eyes. I don't think there is a definite winner between those two (HAML/SLIM).

Erez Rabih
  • 14,472
  • 3
  • 37
  • 59
  • Read it - the last sentence about definitive winner was about HAML and SLIM (edited properly). – Erez Rabih Jul 09 '12 at 07:27
  • 1
    Yep, +1 for this, even though Haml & Slim pull out all the junk from HTML and are for want of a better phrase, totally freakin' awesome, many HTML-only people simply can't get their head around it (or aren't handcoders in the first place, or rely on snippets and copy-pasta.) – ocodo Jan 10 '13 at 05:48
  • 8
    @ErezRabih actually there is a major difference between HAML and SLIM. Slim is much faster than HAML. It also has a cleaner syntax, and allows you to write HTML attributes more cleanly: `a href="foo"`. – Mohamad Nov 20 '13 at 14:17
  • It might look cleaner, but what really matters is the learning curve, and haml looks less intuitive – dev27 Jan 24 '21 at 02:41
  • HAML looks like a bunch of jumbled up words with hashes and dots in front of them. It's horrible. The 2nd problem is that, if you copy something, say a bootstrap code snippet from the internet, you now have to convert it to HAML/SLIM before you can paste it into your code. That increases the development time. Sometimes those conversion tools are not clean, and they also mess up a lot of stuff, and you start wasting time on crying and tearing your hairs instead of getting your code to work - all at the cost of slowing down your code! HAML/SLIM is also slow. – S.Goswami Mar 02 '21 at 10:34
36

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
engineerDave
  • 3,811
  • 22
  • 28
21

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)

montrealmike
  • 10,771
  • 6
  • 56
  • 80
  • 9
    +1. But the pipe isn't needed for text on the same line as the tag. It's only needed if the first line of text inside a tag isn't on the same line as the tag. Every line after the first doesn't need the pipe, because of indentation. – Kelvin Apr 10 '13 at 20:11
  • I actually like this aspect of slim, as it makes it extra difficult for me to write non-internationalized strings. – KonstantinK Sep 24 '14 at 08:20
  • definitely `|` for each block of text over `%` for every tag. There are far more tags than there are blocks of literal text. A small, but semantic win for me with slim is that any raw literal html with `<>` tags used is preceded by an explicit `|`. I prefer "everything is slim unless you explicitly make it literal with `|`" over "everything is literal until you make it haml with a `%`. – ahnbizcad Feb 10 '15 at 09:28
  • I think Slim looks more like HTML (`attr="value"`), while Haml looks more like Ruby (`{key: "value"}` like Hash). – Franklin Yu May 23 '19 at 02:38
17

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."

mahemoff
  • 38,430
  • 30
  • 133
  • 196
ocodo
  • 27,324
  • 15
  • 97
  • 113