Uncomfortable editing ERb / EEx templates? Try Slim / Slime

    ERb is the standard markup language in the Ruby world. This is html with inserts in Ruby. In the world of Elixir, EEx plays the same role. The same syntax, but instead of Ruby - Elixir. Here's what it looks like:


    Заголовок

    <%= page_header %>

    <%= page_content %>

    Дополнительный текст.

    The same can be written 1.5 times shorter without loss of readability.


    Meet: Slim markup language and its Elixir-analogue Slime:



    I apologize for the picture instead of text, Habr incorrectly highlights Slim.


    Features


    • Nesting is controlled by indentation. Anyway, you format the code;
    • Shorthand syntax for identifiers and classes;
    • If the tag name is not specified, it will be substituted div;
    • Attributes can be optionally framed or not framed by brackets:


      meta name="author"
      meta[name="author"]
      meta(name="author")
      meta{name="author"}

    • Supported by text editors. In general, without syntax highlighting, it would be impossible to use slim: the names of tags and their contents would merge. And with the backlight everything is ok.
    • There is integration with popular frameworks ( Rails , Sinatra , Phoenix );
    • It works fast. Here is the benchmark from 2014. Slim appears to be slightly slower than Erb (Slime runs at EEx speed).

    How to transfer the project to Slim?


    There is an online converter ; Suitable if there are not many templates. There is a html2slim gem that will convert all project templates:


    $ gem install html2slim
    $ erb2slim app/views

    How to transfer a project to Slime?


    There are fewer programmers on Elixir than rubists (by the way, what are they called “elixirists”?). Slime is used less often. In general, when I, after spitting an hour on EEx, went to look for the Slim implementation, I discovered the Slime ecosystem in an immature state. Those. the language itself worked, it integrated perfectly with the phoenix, but the overall impression was like a keyboard with a drop-down key.


    Well, this is opensource. Having rolled up my sleeves, I added Slime support to Sublime Text, improved the official website of the language ( slime-lang.com ) and made the eex2slime utility for converting. I checked it on the open source sources hex.pm and changelog.com : due to the small limitations of Slime, the source templates had to be prepared a little, but in general the conversion was successful. This utility is an adaptation of the html2slim gem, so Ruby> = 1.9.3 is needed to work. Using:


    $ gem install eex2slime
    $ eex2slime lib/web/templates/

    I also made an online converter . So far, it only works in slime-> eex mode. Useful for checking how a particular Slime template will work.


    It's worth it?


    Writing and editing code on Slim is faster than on Erb. Of the minuses: not everyone knows him; To some, the syntax seems poorly readable. However, it is easy to learn, and syntax highlighting eliminates possible problems with readability.


    If your project team is small and your colleagues don’t mind, it's worth trying Slim / Slime.


    PS If writing mountains of tags is inconvenient, and Slim doesn't like the syntax, you can try Emmet .


    Also popular now: