Rails & Slim - A match made in haven19 Nov 2017
Rails is about doing thins elegantly, with minimal code, in an extensible fashion. Slim is a template engine that transpile to HTML. Its minimal, elegant and fast. It makes boring HTML fun to write and easy to read! This document includes some helpful information about the world of Slim.
Add slim-rails to your Gemfile and
# Gemfile gem 'slim-rails'
This should do the trick. But for older versions I think you need to configure Rails to use Slim as the default template engine. If that is the case, do the following.
# config/application.rb class Application < Rails::Application # Add the following config.generators do |g| g.template_engine :slim end end
Converting HTML / ERB to Slim
What if you already have your views in ERB? Or what if your designer provided you with HTML. This is more common than you think. Fortunately there is a tool for the rescue. It is html2slim. While the name suggests it converts HTML, it works fine for both HTML and ERB.
# Gemfile group :development do gem 'html2slim' end
This works fine 90% of the time flawlessly. Some times small portions of the conversion fails when it mistakes and adds Ruby in to some HTML attributes. I wanted to fix this long ago, but really didn’t find the time. Just delete the embedded Ruby and you will be fine.
I consider Slim a must in every new project I start. Then there is Haml. It takes a different approach to Slim. Many dislike HAML. How ever if you have an existing project in HAML, stick to it. Otherwise just jump shift to Slim!