📜 ⬆️ ⬇️

Slim Template Designer - Haml Alternative

I have long wanted to try Haml, but still there was no time. But I recently discovered a new template engine, which I immediately liked. According to the creators, he took the best from Jade and Haml. The main task is to reduce the amount of code, while not making it scary and incomprehensible.

This is what the template looks like using Slim:
doctype html html head title Slim Examples meta name="keywords" content="template language" body h1 Markup examples #content.example1 p Nest by indentation = yield - unless items.empty? table - for item in items do tr td = item.name td = item.price - else p No items found #footer | Copyright © 2010 Andrew Stone = render 'tracking_code' script | $(content).do_something(); 


As in Haml, output formatting is indented. Instead of the <% =%> construct, the = sign is used. For calculations without output in html (for example, for if and for ), put the sign - .

Unlike Haml'a, there is no% sign, which are allocated tags. They can immediately write as is. In essence, this is just HTML without <> brackets, which uses padding to denote nested elements. By the way, the number of indents is your choice, but not less than one.
')
List of all operators:

 |    ,     .    ""  . '       ,     . -      Haml,   ,   ,      <% ... %> =     <%= ... %>,    html ='     ,      . ==     ,    " ",    escape_html =='  ,   ,     . /  .         html . /!   html  (<!-- -->),    . 


Attributes and Comments

You can denote id and class like this:
 blockquote id="quote-#{@quote.id}" class="quote" p class="title" = @quote.title p style="padding:1em;" = @quote.body 


For comparison with Haml:
 %blockquote{:id => "quote-#{@quote.id}", :class => "quote"} %p{:class="title"}= @quote.title %p{:style => "padding:1em;"}= @quote.body 


In addition, Slim allows several syntax options:
 /    .   , ,    Haml' #nav.top div id="nav" class="top" /       h1 class=page_header_class = page_header h1{class=page_header_class} = page_header h1[class=page_header_class] = page_header h1(class=page_header_class) = page_header 


Another nice thing is that if the quotes are not specified in the attribute, the variable will be used. From the example a couple of lines above you can see that the page_header_class variable is used .
 #    ,  . a href="#{url_for @user}" = @user.name #        "#{...}" a href=url_for(@user) = @user.name 


If the function returns false, the attribute will not be displayed at all in html (as in Haml):
 option value="Slim" selected=option_selected?("Slim") # -> <option value="Slim"></option> 


You can use interpolation as in Ruby strings:
 body h1 , #{current_user.name} |     #{{content}}   ,    escape_html. 



I really like the way comments work. If you have a block of code to comment out, it’s enough to add just one line, which will affect the whole block.
 #          /.comments - @comments.each do |comment| == render comment 


It is worth considering that the default render method filters the output, so you should put a double equal sign in front of it so that escape_html does not work twice.

Logic-less mode

 Slim::Engine.set_default_options :sections => true 

And that's what you can do with it:
 /     false  empty?,    h1   - article h1 = title 


 /  ,  article   false  empty?     -! article p    


Perhaps, having read the first example, you have a question - where does the 'title' variable come from? Slim himself is trying to find it in several ways.
 / If article.respond_to?(:title) - article /  article.send(:title) h1 = title 

 / If article.respond_to?(:has_key?) and article.has_key?(:title) - article /   article[:title] h1 = title 

 / If article.instance_variable_defined?(@title) - article /       article.instance_variable_get @title h1 = title 

Personally, I did not like this mode, but maybe it will be you who will like it.

What about performance

Templates in rails are cached, so in speed they will lag behind the standard Erb only at the first access to them. Here is a comparative table that shows that Slim certainly will not be a bottleneck in your application:
 # Linux + Ruby 1.9.2, 1000 iterations user system total real (1) erb 0.680000 0.000000 0.680000 ( 0.810375) (1) erubis 0.510000 0.000000 0.510000 ( 0.547548) (1) fast erubis 0.530000 0.000000 0.530000 ( 0.583134) (1) slim 4.330000 0.020000 4.350000 ( 4.495633) (1) haml 4.680000 0.020000 4.700000 ( 4.747019) (1) haml ugly 4.530000 0.020000 4.550000 ( 4.592425) (2) erb 0.240000 0.000000 0.240000 ( 0.235896) (2) erubis 0.180000 0.000000 0.180000 ( 0.185349) (2) fast erubis 0.150000 0.000000 0.150000 ( 0.154970) (2) slim 0.050000 0.000000 0.050000 ( 0.046685) (2) haml 0.490000 0.000000 0.490000 ( 0.497864) (2) haml ugly 0.420000 0.000000 0.420000 ( 0.428596) (3) erb 0.030000 0.000000 0.030000 ( 0.033979) (3) erubis 0.030000 0.000000 0.030000 ( 0.030705) (3) fast erubis 0.040000 0.000000 0.040000 ( 0.035229) (3) slim 0.040000 0.000000 0.040000 ( 0.036249) (3) haml 0.160000 0.000000 0.160000 ( 0.165024) (3) haml ugly 0.150000 0.000000 0.150000 ( 0.146130) (4) erb 0.060000 0.000000 0.060000 ( 0.059847) (4) erubis 0.040000 0.000000 0.040000 ( 0.040770) (4) slim 0.040000 0.000000 0.040000 ( 0.047389) (4) haml 0.190000 0.000000 0.190000 ( 0.188837) (4) haml ugly 0.170000 0.000000 0.170000 ( 0.175378) 1.      .   ,   slow=1. 2.  .   .  Ruby         .     API . 3.  .    ,   ,  Ruby    .    ,          . 4.  Tilt-.     Tilt,              Sinatra, Ramaze and Camping. 

How to install

There are 2 options. The first is without adding template generators (to create, for example, Scaffolds). The second is with generators.
 #     Slim gem 'slim' #    Slim   Scaffold' gem 'slim-rails' 
Then you need to register in the console bundle install to install the selected gems.

Do not forget that to use Slim your files must have the extension .slim . That is, the index.html.erb file will be executed by the Erb template engine, and index.html.slim , respectively, by Slim.

useful links

slim-lang.com - official page
github.com/stonean/slim - page on Github
github.com/fredwu/haml2slim - Haml to Slim Converter
github.com/fredwu/ruby-slim-tmbundle - bandl for TextMate
github.com/bbommarito/vim-slim files for Vim

Source: https://habr.com/ru/post/128645/


All Articles