Blog Posts: Latest Trends and Insights in Technologies | Clarion Technologies

Revisiting template engines in ruby on rails

Written by Clarion Tech | Jan 10, 2015 6:30:00 PM

Being a developer, we always have the inclination to boost our application performance by concentrating on various technical areas like Caching, Database Optimization, Scaling out Database, Load Balancing, Front-end optimization, Minify and GZip stylesheets and javascripts etc.

Most of the developers focus on scaling development & deployment process rather than presentation layer of the application. Let us try to look into this aspect as well. You may have heard of various template engines available with ruby like Erubis, Tilt, HAML, Slim, Liquid, Mustache, temple, deface and many more. This blog could be helpful to those who are interested in using the template engines to improve the optimization of the presentation layer of the application. In this article, I would like to bring out "Slim" template in comparison with the most famous template engines. Slim, which is born to bring a minimalist syntax approach to speed. ERB and HAML are the most popular template engines within the Rails community. However, Erb's syntax is cumbersome and Haml's syntax can be quite cryptic to the novice users.

To help others here is a brief comparison of all three: ERB-vs-HAML-vs-Slim

1. What is it?

  • ERB:

    • ERB is the Ruby language's built-in template engine.
    • Uses expression tags <%= ... %> and scriptlet tags <% ... %>
    • Useful for embedding Ruby in any kind of source document, most commonly HTML and XML.
  • HAML:

    • HAML is a template engine designed to provide a layer of abstraction above HTML.
    • It uses a clean, concise syntax which compiles down to HTML and offers several usability benefits.
  • Slim:

    • Slim is another lightweight templating engine.
    • Derived from HAML. The primary implementation is in Ruby.
    • Offers extended functionality with an arguably more expressive syntax and a quicker compilation speed.

     

2. Pros

  • ERB:

    • Familiar HTML-style syntax.
    • Simple learning curve
    • Easy to figure out what a given tag is doing
    • Not white space dependent
    • Fastest parsing speed
  • HAML:

    • DRY
    • Short, easy tags
    • White-space scoping
    • Well-formatted markup
    • Follows CSS conventions
    • Integrates Ruby code
  • Slim:

    • Elegant syntax
    • Highly configurable
    • High performance
    • Logic less mode similar to Mustache

     

3. Cons

  • ERB:

    • Verbose syntax format and more clutter!
    • Content_for tags make 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:

    • Parsing is slower than Erb
    • The syntax can be minimized further; attributes still require hashes, for example.
    • Haml is 2.8 times slower than Erb and 8 times slower than Slim
    • Whitespace dependent which makes for some hard errors to figure out at times.
    • Designers might have some trouble adjusting.

     

  • Slim:

    • Designers might have some trouble adjusting

     

4. Which should you choose?

  • ERB:

    • You don't have time to learn a new syntax.
    • You don't want to add additional gems
    • You don't care for prettier code

     

  • HAML:

    • You need lightning-fast templates.
    • More common and good choices.

     

  • Slim:

    • You need better speed and extended functionality.
    • You want to code in more readable and concise.
    • You need HTTP streaming
    • You want to work with Logic less mode

     

5. Snippet

  • ERB: 

    <p class="code" data-attrib="var"><%= code %></p>

  • HAML: 

    HAML is a template engine designed to provide a layer of abstraction above HTML. It uses a clean, concise syntax which compiles down to HTML and offers several usability benefits.%p.code{"data-attrib" => "var"}= code

  • Slim: 

    p.code data-attrib="var" = code