A more suitable HTML templating system would:
- be tightly focused on generating HTML pages
- demand correct intendation
- reduce HTML tag verbosity and therefore increase readability
It is easy to integrate by adding:
gem 'haml'to the Gemfile.
A simple user.html.erb file:
<h1>Welcome <%= @user.name %></h1> <div id="friends"> <% @user.friends.each do |friend| %> <div id="<%= dom_id friend %>" class="friend"> <%= friend.name %> </div> <% end %> </div>can refactored by creating the user.html.haml containing:
%h1= @user.name #friends - @user.friends.each do |friend| .friend{ id: dom_id(friend) }= friend.nameThe result is way shorter. HAML is well documented.
Please note that HAML forces you to take care of correct indentation. Otherwise it might crash with errors like:
Inconsistent indentation: 6 spaces used for indentation, but the rest of the document was indented using 4 spaces.when there is a indent missing.
Or a little less intuitive:
SyntaxError - syntax error, unexpected keyword_ensure, expecting keyword_endwhen indenting a Ruby block (like an interator) was incorrect.
Or even harder to get is unintentional DOM structure by wrong indentation.
Anyway HAML forces to indent correctly because whitespaces matter. And that is a benefit, because it helps to write cleaner HTML.
Further articles of interest:
Supported by Ruby 2.1.1 and Ruby on Rails 4.1.8
Keine Kommentare:
Kommentar veröffentlichen