Mind Dump, Tech And Life Blog
written by Ivan Alenko
published under license Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)copy! share!
posted at 21. Apr '18

Template engines of Ruby

ERb (Ruby STL or erubis or erubi implementation)

It stands probably for embedded Ruby and it has similar syntax like PHP - the basic code is HTML and via <%, <%= and %> (and some other control tags) it is possible to run any Ruby code.

Most of the time it is simple, but can get messy if you are doing a lot of database queries and xxx.each do |x|… It may be better to use partial rendering on collection.

render partial: 'page/show', collection: @pages

And it is the best for playing with code (like the third attempt to implement flexboxes). It is possible to change code or CSS classes and identifiers very fast and messing with indentation like in HAML and Slim is very irritating. One of the disadvantages may be, that syntax hightlighting doesn’t work with editors like Kate or Scribes.

+ fast
+ simple
+ flexible
- can be messy
+- PHP like syntax

HAML [haml.info]

The next step the Ruby programmer makes, is to notice that ERb is too messy and verbose. And bumps into HAML. I think the manual describes the process of converting to HAML is that you press backspace and delete characters. Because HAML depends on indentation, lines like <% end %> or </div> aren’t necessary.

The disadvatage is, that HAML used to sort attributes (in Ruby 1.8 I think), and you are a bit limited to do more complex structure in the document. Which is fine, but many times irritating (mostly in tags). And it doesn’t properly indent source code by default. I like nice source.

+- enforce structure
+ has a lot less characters
- debug (sometimes)
- enforces own style of HTML tags
- rendered source code is ugly by default

Slim language [slim-lang.com]

It is very similar to HAML, but I would say that it provides much more freedom and fun. Well, the main difference is, that lines with text must begin with , but HTML tags don’t have have % prefix. The main disadvantage in Slim is debugging. A lot of times it simply cannot show proper line in the view and points to another. You have to use your sixth sense to determine correct line. And there is the bug with Unicode characters, when it shows “Something has gone wrong” instead of stack trace.
advantages and disadvantages are the same as HAML
+ more fun and freedom than HAML
+ really, really much more fun

Add Comment