Haml And Sass In 15 Minutes

72
Patrick Crowley the.railsi.st

Transcript of Haml And Sass In 15 Minutes

Page 1: Haml And Sass In 15 Minutes

Patrick Crowleythe.railsi.st

Page 2: Haml And Sass In 15 Minutes

Haml & Sass in 15 minutes

Page 3: Haml And Sass In 15 Minutes

What are Haml and Sass?

Page 4: Haml And Sass In 15 Minutes
Page 5: Haml And Sass In 15 Minutes

• Template languages

Page 6: Haml And Sass In 15 Minutes

• Template languages

• Haml generates .html

Page 7: Haml And Sass In 15 Minutes

• Template languages

• Haml generates .html

• Sass generates .css

Page 8: Haml And Sass In 15 Minutes

• Template languages

• Haml generates .html

• Sass generates .css

• You can use one or both

Page 9: Haml And Sass In 15 Minutes

Do I have to learn another markup language?

Page 10: Haml And Sass In 15 Minutes

Yes, you do. Get over it.

Page 11: Haml And Sass In 15 Minutes

“Haml is poetry.”Max Muermann

Page 12: Haml And Sass In 15 Minutes

The principles of Haml...

Page 13: Haml And Sass In 15 Minutes
Page 14: Haml And Sass In 15 Minutes

• Markup should be beautiful

Page 15: Haml And Sass In 15 Minutes

• Markup should be beautiful

• Markup should be DRY

Page 16: Haml And Sass In 15 Minutes

• Markup should be beautiful

• Markup should be DRY

• Markup should be indented

Page 17: Haml And Sass In 15 Minutes

• Markup should be beautiful

• Markup should be DRY

• Markup should be indented

• Structure should be clear

Page 18: Haml And Sass In 15 Minutes

The Basics

Page 19: Haml And Sass In 15 Minutes
Page 20: Haml And Sass In 15 Minutes

• White space active

Page 21: Haml And Sass In 15 Minutes

• White space active

• Indentation = structure

Page 22: Haml And Sass In 15 Minutes

• White space active

• Indentation = structure

• Tags begin with %

Page 23: Haml And Sass In 15 Minutes

• White space active

• Indentation = structure

• Tags begin with %

• Tags close themselves

Page 24: Haml And Sass In 15 Minutes

• White space active

• Indentation = structure

• Tags begin with %

• Tags close themselves

• Use hashes for attributes

Page 25: Haml And Sass In 15 Minutes

<h1>Haml is cool</h1>

Page 26: Haml And Sass In 15 Minutes

%h1 Haml is cool

Page 27: Haml And Sass In 15 Minutes

<h1>Haml is cool, Lisa</h1>

Page 28: Haml And Sass In 15 Minutes

<h1>Haml is cool, <%= @name %></h1>

Page 29: Haml And Sass In 15 Minutes

%h1 = "Haml is cool, #{@name}"

Page 30: Haml And Sass In 15 Minutes

<div id="color">Red</h1>

Page 31: Haml And Sass In 15 Minutes

%div#color Red

Page 32: Haml And Sass In 15 Minutes

#color Red

Page 33: Haml And Sass In 15 Minutes

Loops

Page 34: Haml And Sass In 15 Minutes

<ul id="friends"><% @friends.each do |friend| %><li><%= friend.name %></li><% end %></ul>

Page 35: Haml And Sass In 15 Minutes

%ul#friends- @friends.each do |friend| %li= friend.name

Page 36: Haml And Sass In 15 Minutes

<ul id="friends"> <li>Ted</li> <li>Erin</li> <li>Gerry</li></ul>

Page 37: Haml And Sass In 15 Minutes

Attributes

Page 38: Haml And Sass In 15 Minutes

ul#friends- @friends.each do |friend| %li= friend.name

Page 39: Haml And Sass In 15 Minutes

ul{:id => "friends", :class => "list"}- @friends.each do |friend| %li= friend.name

Page 40: Haml And Sass In 15 Minutes

<ul id="friends" class="list"> <li>Ted</li> <li>Erin</li> <li>Gerry</li></ul>

Page 41: Haml And Sass In 15 Minutes

Forms

Page 42: Haml And Sass In 15 Minutes

<% if logged_in? -%>

<% form_for :comment, :url => blog_comments_path(@post) do |f| %>

<label for="comment_comment">Post a comment:</label> <%= f.text_area :comment, :rows => 14, :cols => 40 %>

<%= submit_tag "Add comment" %>

<% end -%>

<% else -%>

<%= link_to "Login", :action => "login" %>

<% end -%>

Page 43: Haml And Sass In 15 Minutes

if logged_in? - form_for :comment, :url => blog_comments_path(@post) do |f|

%label{:for => "comment_comment"} Post a comment: = f.text_area :comment, :rows => 14, :cols => 40 = submit_tag "Add comment"

- else = link_to "Login", :action => "login"

Page 44: Haml And Sass In 15 Minutes

Layouts

Page 45: Haml And Sass In 15 Minutes

<head><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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 :site => "Graffletopia", :separator => "-" %><%= stylesheets %><%= javascript_include_tag :defaults %></head><body>

<% if logged_in? %><p>You are logged in.</p><% end %>

<% if flash[:notice] != nil -%><p><%= flash[:notice] %></p><% end -%>

<%= yield %>

</body></html>

Page 46: Haml And Sass In 15 Minutes

!!! Strict%html{html_attrs} %head = title :site => "Graffletopia", :separator => "-" %meta{:http-equiv => "Content-type", :content => "text/html;charset=UTF-8"} = stylesheets = javascript_include_tag :defaults %body - if logged_in? %p You are logged in. - if flash[:notice] != nil %p= flash[:notice] = yield

Page 47: Haml And Sass In 15 Minutes

Sass

Page 48: Haml And Sass In 15 Minutes

Basic syntax

Page 49: Haml And Sass In 15 Minutes

#box { border: 0; color: black; }

Page 50: Haml And Sass In 15 Minutes

#box border: 0 color: black

Page 51: Haml And Sass In 15 Minutes

#box :border 0 :color black

Page 52: Haml And Sass In 15 Minutes

Nesting

Page 53: Haml And Sass In 15 Minutes

#box :border 0 :color black .orange :border 1px orange

Page 54: Haml And Sass In 15 Minutes

#box { border: 0; color: black; }

#box .orange { border: 1px orange; }

Page 55: Haml And Sass In 15 Minutes

Variables

Page 56: Haml And Sass In 15 Minutes

!pink = #f3f#box :border 0 :color black .pink :border = !pink

Page 57: Haml And Sass In 15 Minutes

#box { border: 0; color: black; }

#box .pink { color: #f3f; }

Page 58: Haml And Sass In 15 Minutes

Comments

Page 59: Haml And Sass In 15 Minutes

/* Homepage box#box :border 0 :color black

Page 60: Haml And Sass In 15 Minutes

Now go play!

Page 61: Haml And Sass In 15 Minutes

script/plugin install http://svn.hamptoncatlin.com/haml/tags/stable

Page 62: Haml And Sass In 15 Minutes

Helpful hints

Page 63: Haml And Sass In 15 Minutes
Page 64: Haml And Sass In 15 Minutes

• Grab the TextMate bundles

Page 65: Haml And Sass In 15 Minutes

• Grab the TextMate bundles

• Start a template at a time

Page 66: Haml And Sass In 15 Minutes

• Grab the TextMate bundles

• Start a template at a time

• Use .haml or .html.haml

Page 67: Haml And Sass In 15 Minutes

• Grab the TextMate bundles

• Start a template at a time

• Use .haml or .html.haml

• Use .sass

Page 68: Haml And Sass In 15 Minutes

• Grab the TextMate bundles

• Start a template at a time

• Use .haml or .html.haml

• Use .sass

• Screw up? Check whitespace

Page 69: Haml And Sass In 15 Minutes

• Grab the TextMate bundles

• Start a template at a time

• Use .haml or .html.haml

• Use .sass

• Screw up? Check whitespace

• Move logic to helpers

Page 70: Haml And Sass In 15 Minutes

Have fun!

Page 71: Haml And Sass In 15 Minutes

The End

Page 72: Haml And Sass In 15 Minutes