Haml, an alternative for views

suggest change

Haml (HTML Abstraction Markup Language) describes the HTML of your views. Instead of opening and closing tags, Haml uses indentation. If something sits inside another element, indent it one level. Keep the indentation consistent. Two spaces per level is the usual choice.

Examples:

<%# myview.html.erb %>
<h1><%= @the_title %></h1>
<p>This is my form</p>
<%= render "form" %>

And in Haml:

-# myview.html.haml
%h1= @the_title
%p
  This is my form
= render 'form'

The structure is easier to scan than HTML and ERB.

Installation

Add the gem to the Gemfile and run bundle install. gem install alone does not make the app load it.

gem "haml"

To use Haml instead of HTML/ERB, change view extensions from something.html.erb to something.html.haml.

Quick tips

A div can be written in a short form.

HTML

<div class="myclass">My Text</div>

Haml

%div.myclass My Text

Haml, shorthand (div is the default tag)

.myclass My Text

Attributes

HTML

<p class="myclass" id="myid">My paragraph</p>

HAML

%p{:class => "myclass", :id => "myid"} My paragraph

Inserting Ruby code

Insert Ruby with = and -.

= link_to "Home", home_path

A line starting with = is executed and written into the document.

A line starting with - is executed and not written into the document.

Full documentation

Haml is easy to start and has more syntax than this page shows. Read the Haml reference.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents