23
How’s Haml? Patrick Reagan

Hows Haml?

Embed Size (px)

DESCRIPTION

Patrick Reagan gives a quick (~15 minute) introduction to Haml and how it can be used in Ruby web applications.

Citation preview

Page 1: Hows Haml?

How’s Haml?Patrick Reagan

Page 2: Hows Haml?

What is Haml?

• Templating language for creating XHTML

• Alternative to ERB in Rails

• Principle: “Markup should be beautiful”

Page 3: Hows Haml?

What is Beauty?

Page 4: Hows Haml?

What is Beauty?

Page 5: Hows Haml?

Installation

$ sudo gem install --no-ri hamlSuccessfully installed haml-1.8.01 gem installedInstalling RDoc documentation for haml-1.8.0...

$ haml --rails ./my_rails_app Haml plugin added to ./my_rails_app

Page 6: Hows Haml?

Using Haml

app/views/posts: index.html.erb => index.html.hamlapp/views/layouts: application.html.erb => application.haml

Rename views and layouts

Start deleting!

Page 7: Hows Haml?

ERB / RHTML<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Yet Another Rails Blog</title> </head> <body> <div id='container'> <div id='navigation'> <%= link_to 'Home', root_path %> </div> <div id='content'> <% @posts.each do |post| -%> <div class='post'> <h3><%= h post.title %></h3> <strong>by: <%= post.author %></strong> <p><%= post.body %></p> </div> <% end %> </div> </div> </body></html>

Page 8: Hows Haml?

ERB / RHTML<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Yet Another Rails Blog</title> </head> <body> <div id='container'> <div id='navigation'> <%= link_to 'Home', root_path %> </div> <div id='content'> <% @posts.each do |post| -%> <div class='post'> <h3><%= h post.title %></h3> <strong>by: <%= post.author %></strong> <p><%= post.body %></p> </div> <% end %> </div> </div> </body></html>

!!! XML!!! Strict%html{html_attrs}

Page 9: Hows Haml?

ERB / RHTML<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Yet Another Rails Blog</title> </head> <body> <div id='container'> <div id='navigation'> <%= link_to 'Home', root_path %> </div> <div id='content'> <% @posts.each do |post| -%> <div class='post'> <h3><%= h post.title %></h3> <strong>by: <%= post.author %></strong> <p><%= post.body %></p> </div> <% end %> </div> </div> </body></html>

!!! XML!!! Strict%html{html_attrs}

%head %title Yet Another Rails Blog %body

Page 10: Hows Haml?

ERB / RHTML<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Yet Another Rails Blog</title> </head> <body> <div id='container'> <div id='navigation'> <%= link_to 'Home', root_path %> </div> <div id='content'> <% @posts.each do |post| -%> <div class='post'> <h3><%= h post.title %></h3> <strong>by: <%= post.author %></strong> <p><%= post.body %></p> </div> <% end %> </div> </div> </body></html>

!!! XML!!! Strict%html{html_attrs}

%head %title Yet Another Rails Blog %body

#container #navigation = link_to('Home', root_path)

Page 11: Hows Haml?

<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Yet Another Rails Blog</title> </head> <body> <div id='container'> <div id='navigation'> <%= link_to 'Home', root_path %> </div> <div id='content'> <% @posts.each do |post| -%> <div class='post'> <h3><%= h post.title %></h3> <strong>by: <%= post.author %></strong> <p><%= post.body %></p> </div> <% end %> </div> </div> </body></html>

!!! XML!!! Strict%html{html_attrs}

%head %title Yet Another Rails Blog %body

#container #navigation = link_to('Home', root_path)

#content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body

Hamlified!

Page 12: Hows Haml?

Haml Deconstructed

!!! XML!!! Strict

%html{html_attrs} %head %title Yet Another Rails Blog %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body

Page 13: Hows Haml?

Haml Deconstructed

!!! XML!!! Strict

%html{html_attrs} %head %title Yet Another Rails Blog %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body

XHTML document type

Page 14: Hows Haml?

Haml Deconstructed

!!! XML!!! Strict

%html{html_attrs} %head %title Yet Another Rails Blog %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body

XHTML document type

XHTML tag with static content

Page 15: Hows Haml?

Haml Deconstructed

!!! XML!!! Strict

%html{html_attrs} %head %title Yet Another Rails Blog %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body

XHTML document type Haml helper method

XHTML tag with static content

Page 16: Hows Haml?

Haml Deconstructed

!!! XML!!! Strict

%html{html_attrs} %head %title Yet Another Rails Blog %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body

XHTML document type Haml helper method

XHTML tag with static content

DIV tag with ID

Page 17: Hows Haml?

Haml Deconstructed

!!! XML!!! Strict

%html{html_attrs} %head %title Yet Another Rails Blog %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body

XHTML document type Haml helper method

XHTML tag with static content

DIV tag with ID

Execute Ruby code

Page 18: Hows Haml?

Haml Deconstructed

!!! XML!!! Strict

%html{html_attrs} %head %title Yet Another Rails Blog %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body

XHTML document type Haml helper method

XHTML tag with static content

DIV tag with ID

Execute Ruby code

XHTML tag withdynamic content

Page 19: Hows Haml?

XHTML Output<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Yet Another Rails Blog</title> </head> <body> <div id='container'> <div id='navigation'> <a href="/">Home</a> </div> <div id='content'> <div class='post'> <h3>January Refresh Recap</h3> <strong>by: Patrick</strong> <p>It was awesome!</p> </div> ... </div> </div> </body></html>

Page 20: Hows Haml?

Off the Rails?

$ irb >> require 'rubygems' => true >> require 'haml' => true >> Haml::Engine.new('%p Hello, World').render => "<p>Hello, World</p>\n"

Page 21: Hows Haml?

Why Use It?

• Creates well-formatted markup

• Automatic closing of tags

• Less “noise”

Page 22: Hows Haml?

Why Not?

• Buildout must happen inside Rails

• XHTML is a widespread standard

• Performance

Page 23: Hows Haml?

ResourcesOther templating systems

• Erubis

• Markaby

• pHAML

Tutorial & Documentation

• http://haml.hamptoncatlin.com/tutorial/

• http://haml.hamptoncatlin.com/docs/

TextMate Bundle (via SVN)• http://macromates.com/svn/Bundles/trunk