An introduction to haml
haml is template languages for Ruby on Rails. It's a plugin that provides an alternative to Rails' native view templating library erb. sass is included with haml, and provides templating for css files. Through clever usage of whitespace, both remove much of the verbosity of html and css (<>, end tags ,etc) , providing a concise way of creating templates. For example, take this typical layout template:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title><%= controller.action_name %></title>
<%= stylesheet_link_tag 'mycss' %>
</head>
<body>
<p style="color: green">
<%= flash[:notice] %>
</p>
<div id="header">
<h1>Header</h1>
</div>
<div id="content">
<%= yield %>
</div>
</body>
</html>
Here's the same thing in haml. Notice the usage of whitespace to delimit block the nesting of the code. Also, regular html can be mixed in as well.
!!!
%html{:xmlns => "http://www.w3.org/1999/xhtml", "xml:lang" => "en", :lang => "en"}
%head
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
%title= controller.action_name
= stylesheet_link_tag 'mycss'
%body
%p{:style => "color:green"}= flash[:notice]
#header
%h1 Header
#content
= yield
There are a few benefits to this:
- the information density is greater, so more of the template is visible on screen.
- less characters to type, so less chance to make a typo
- nesting is clearly visible, and no more issues with missing end tags.



Post Comments