Adding
Haml

Nick Walsh

Envy Labs

@nickawalsh

Adding Haml

The three chief virtues of a
programmer are: Laziness,
Impatience and Hubris.

Larry Wall, co-author Programming Perl

What
is Haml?

HTML
Abstraction
Markup
Language

Function & Features

The First Level

!!! 5
%html(lang="en")
  %head
    %title A Real Human Being
  %body
    #content.group
      %h1.headline And a Real Hero

Pre-reqs

• HTML

Deceptive
Gains

History

All of Hashrocket's new projects are done in Haml, and we've now decided to transition everything else in our portfolio over to Haml as soon as possible.

Obie Fernandez, co-founder Hashrocket, author The Rails Way

Who's Responsible

cleaner and DRY-er
write HTML haiku
templating fun. Scraunched*

(Don't worry, this'll be the only haiku.)

HTML
to
Haml

Won't
Someone
Think
of the
Designers?

(They're
in the
Audience)

A Quick Note on the Examples

Basic Tags

%h1 But You're Still the Same

Basic Tags

%p Good.
%p
  You're just sayin' that?

Illegal Nesting

%h3 A Real Hero
  %b EP

Doctypes

!!! 5
+
!!! Transitional
+
!!! XML

Attributes

%p{rel: 'lb', title: 'A Link'}
  What do you do?
%p(rel="lb" title="A Link")
  I drive, for movies.

IDs & Classes

%p(id="header" class="group")
  Is that dangerous?
%p#footer.group
  It's only part time.

Divs Aplenty

%div.group
  Sorry about the noise.
.group
  I was gonna call the cops.
.group(rel="lb")
  I wish you would.

Nesting

%header
  %h1 Hey Kid
  %h2 You want a toothpick?
  %nav
    %a(href="#" title="A Link")
      Toothpick Emporium

Nesting

%header
    %h1 Hey Kid
  %h2 You want a toothpick?

Comments (HTML)

/ A standard comment

Comments (Conditional)

/ [lte IE8]
  %h1 Congratulations!

Comments (Haml)

-# %section
  %p
    %em First!

Self-Closing Tags

%br/
%br

Filters Everywhere

Haml Filters

JavaScript Filter

:javascript
  var x = 42;
  alert(x);

Adding
the
Server
Side

Embedding Ruby

- color = "green"
= color

Embedding Ruby

- color = "green"
- if color == "green"
  = color

The Good

So, Haml's Kinda Great

Universal Selectors

Uniformity

Reduces Instances of This:

<div>
  <h1>A Title</h1>
<form>
  <p>
  <label>A Label</label>
  <input type="text" id="input" />
    </p>
</form>
</div>

(They'll Probably See This at Some Point)

The Bad

Haml is Not for Content Formatting

Content Formatting

%p
  Call me Ishmael.
  %span Some years ago —
  never mind how long 
  %em precisely

Content Formatting

%p
  Call me <em>Ishmael</em>. Some years ago …

Solution: Markdown

:markdown
  ## Heading 2
  1. List Item 1
  2. List *Item 2*
  
  [A Link](http://haml-lang.org)

Cleaning Up Output

Multiline

%p
  noiseless nautilus,
  light prows
%p
  noiseless nautilus, |
  light prows |

Whitespace (Inside)

%h1
  %span The Pacific
%h1<
  %span The Symphony

Whitespace (Outside)

%h1
  %b The
  %em> Chase
  First

Whitespace (Both)

.class
  %h1><
    %span Title

The Ugly Option

!!! 5
%html(lang="en")
  %head
    %title A Real Human Being
  %body
    #id.class
      %h1.class And a Real Hero

The Install

So, You're On Board

Basic Usage

Static Site Usage

Rails

What About
My Favorite
Language?

Syntax Highlighting

Q&A

Resources & Credits