haml, sass and compass for sane web development

55
Haml, Sass and Compass for Sane Web Development Jeremy Weiskotten @doctorzaius

Upload: jeremyw

Post on 28-Jan-2015

135 views

Category:

Technology


1 download

DESCRIPTION

HTML and CSS are ubiquitous on the web, but they were mostly created in a vacuum. Haml, Sass, and Compass provide helpful abstractions and utilities to remove some of the repetition and noise in your code for more rapid development and easier maintenance.This talk was given at the Boston Ruby Group on September 14, 2010.

TRANSCRIPT

Page 1: Haml, Sass and Compass for Sane Web Development

Haml, Sass and Compass for Sane Web DevelopmentJeremy Weiskotten

@doctorzaius

Page 2: Haml, Sass and Compass for Sane Web Development

(We’re hiring.)

QuickTime™ and a decompressor

are needed to see this picture.

Page 3: Haml, Sass and Compass for Sane Web Development

Haml

Page 4: Haml, Sass and Compass for Sane Web Development

QuickTime™ and a decompressor

are needed to see this picture.

Page 5: Haml, Sass and Compass for Sane Web Development

QuickTime™ and a decompressor

are needed to see this picture.

Page 6: Haml, Sass and Compass for Sane Web Development

QuickTime™ and a decompressor

are needed to see this picture.

Mark Hamill may not be beautiful, but…

Page 7: Haml, Sass and Compass for Sane Web Development

“Markup should be beautiful.”

http://haml-lang.com

Page 8: Haml, Sass and Compass for Sane Web Development

Is this beautiful?

Page 9: Haml, Sass and Compass for Sane Web Development

No.

Page 10: Haml, Sass and Compass for Sane Web Development

QuickTime™ and a decompressor

are needed to see this picture.

Page 11: Haml, Sass and Compass for Sane Web Development
Page 12: Haml, Sass and Compass for Sane Web Development
Page 13: Haml, Sass and Compass for Sane Web Development
Page 14: Haml, Sass and Compass for Sane Web Development

HTML Tags

%p My paragraph.

%span= @user.first_name

%ul %li Item 1 %li Item 2

Page 15: Haml, Sass and Compass for Sane Web Development

Tags with IDs and Classes

%p#summary

%span.hint

%p#summary.magnificent.bastard

Page 16: Haml, Sass and Compass for Sane Web Development

Tags with Attributes

%img{:src => ‘images/tree.jpg’, :alt => ‘My favorite tree’}

Produces…

<img src=“images/tree.jpg alt=“My favorite tree” />

Page 17: Haml, Sass and Compass for Sane Web Development

Tags with Attributes

%a.new{:href => new_thing_path} Click me!

Produces…

<a class=“new” href=“/things/new”>Click me!</a>

Page 18: Haml, Sass and Compass for Sane Web Development

Tags withHTML 5 Data Attributes%ul.todo %li{:data => { :priority => 1, :cost => 0 }} Kick ass %li{:data => { :priority => 2, :cost => 1 }} Chew bubble gum

<ul class=“todo”> <li data-priority=“1” data-cost=“0”>Kick

ass</li> <li data-priority=“2” data-cost=“1”>Chew bubble

gum</li></ul>

Page 19: Haml, Sass and Compass for Sane Web Development

Attributes, HTML-style

%img(src=‘images/tree.jpg’ alt=‘My favorite tree’)

%a.new(href=new_thing_path) Click me

Page 20: Haml, Sass and Compass for Sane Web Development

Attribute helpers

%body{ body_attributes }

def body_attributes { :id => controller.controller_name, :class => controller.action_name }end

Page 21: Haml, Sass and Compass for Sane Web Development

Attribute helpers

%li{ user_li_attributes(person) }

def user_li_attributes(user) {}.tap do |attr| attr[:id] = dom_id(user) attr[:class] = ‘active’ if user == current_user endend

Page 22: Haml, Sass and Compass for Sane Web Development

Flow & Output

- if signed_in? = current_user.first_name- else = link_to ‘Sign in’, sign_in_path

- content_for :head do = javascript_include_tag ‘foo’

Page 23: Haml, Sass and Compass for Sane Web Development

QuickTime™ and a decompressor

are needed to see this picture.

Not.

Page 24: Haml, Sass and Compass for Sane Web Development

Haml Sucks for Content *

• Whitespace pain, particularly around punctuation

• Content is not structure• Use a filter for inline content markup

* See http://chriseppstein.github.com/blog/2010/02/08/haml-sucks-for-content/

Page 25: Haml, Sass and Compass for Sane Web Development

Content Filters

•:textile, :markdown, :maruku, etc.

.summary :textile h1. Welcome, #{current_user.name}.

If you’re not, *#{current_user.name}*, “sign out”:#{sign_out_path}!

Page 26: Haml, Sass and Compass for Sane Web Development

JavaScript Filter

:javascript var annoyingMsg = ‘Welcome, #{current_user.first_name}!’;

$(function() { $(‘#welcome’).html(annoyingMsg); });

Page 27: Haml, Sass and Compass for Sane Web Development

So why do I like Haml?

Page 28: Haml, Sass and Compass for Sane Web Development

So why do I like Haml?

• Clean, well-structured source– Easy to write, read & maintain

Page 29: Haml, Sass and Compass for Sane Web Development

So why do I like Haml?

• Clean, well-structured source– Easy to write, read & maintain

• Encourages good practices– Minimal code in the view– Extract helpers, partials

Page 30: Haml, Sass and Compass for Sane Web Development

So why do I like Haml?

• Clean, well-structured source– Easy to write, read & maintain

• Encourages good practices– Minimal code in the view– Extract helpers, partials

• Better mental mapping to CSS

Page 31: Haml, Sass and Compass for Sane Web Development

gem install haml

Page 32: Haml, Sass and Compass for Sane Web Development

Sass

Page 33: Haml, Sass and Compass for Sane Web Development

CSS that doesn’t suck(as much)

Page 34: Haml, Sass and Compass for Sane Web Development

Sass is a big deal.

QuickTime™ and a decompressor

are needed to see this picture.

Page 35: Haml, Sass and Compass for Sane Web Development

Typical CSS

Page 36: Haml, Sass and Compass for Sane Web Development

Sass Nested Rules

Page 37: Haml, Sass and Compass for Sane Web Development

Parent Selector

a { text-decoration: none; &:hover { text-decoration: underline;

}}

Page 38: Haml, Sass and Compass for Sane Web Development

Typical CSS

Page 39: Haml, Sass and Compass for Sane Web Development

Sass Mixin

Page 40: Haml, Sass and Compass for Sane Web Development

Variables

$highlight-color: #ff0;

span.highlight { background-color: $highlight-color;}

Page 41: Haml, Sass and Compass for Sane Web Development

OMG COLOR MATH!

$highlight-color: #ff0;

span.highlight { background-color: $highlight-color; color: darken($highlight-color, 70%);}

Page 42: Haml, Sass and Compass for Sane Web Development

Other Kinds of Functions

• Color saturation, hue• Opacity• Math (round, ceil, floor, abs)

• Units• Extensible: Add your own

Page 43: Haml, Sass and Compass for Sane Web Development

What does it mean?

• CSS programmability• Composition• Reuse• Organization• Better separation of presentation from structure and content

Page 44: Haml, Sass and Compass for Sane Web Development

A few more things...

• @import• @extend• @if / @else• @for, @while

Page 45: Haml, Sass and Compass for Sane Web Development

http://sass-lang.com

QuickTime™ and a decompressor

are needed to see this picture.

Page 46: Haml, Sass and Compass for Sane Web Development

Compass

Page 47: Haml, Sass and Compass for Sane Web Development

It’s a framework

• …for frameworks• Built on Sass• Easy integration with Rails, Rack, Ramaze, Sinatra, …

• Fresh, minty breath

Page 48: Haml, Sass and Compass for Sane Web Development

Lots of toys!

QuickTime™ and a decompressor

are needed to see this picture.

Page 49: Haml, Sass and Compass for Sane Web Development

With the Having of Many Useful Mixins

• Border radius• Box shadow• Float clearing (without markup)

• Image replacement

Page 50: Haml, Sass and Compass for Sane Web Development

I don’t even know what most of these things

are• Resets• CSS3 transitions• Sticky footer• Table striping• And lots more!

Page 51: Haml, Sass and Compass for Sane Web Development

Grid Systems

Without Class-plosion• Blueprint• 960.gs• Susy• YUI• etc

Page 52: Haml, Sass and Compass for Sane Web Development

Fancy Buttons

Page 53: Haml, Sass and Compass for Sane Web Development

QuickTime™ and a decompressor

are needed to see this picture.

.logo { background: sprite-image("lemonade/lemonade-logo.png");}.lime { background: sprite-image("lemonade/lime.png");}.coffee { background: sprite-image("other-drinks/coffee.png") no-repeat;}

Lemonade

Page 54: Haml, Sass and Compass for Sane Web Development

Questions?

• Did you write these?– No. Credit to Hampton Catlin, Nathan Weizenbaum, Chris Eppstein, and others.

• How can I learn more?– haml-lang.com, sass-lang.com,compass-style.org

• Why did Mark Hamill let himself go?– The 80s were hard on all of us.

Page 55: Haml, Sass and Compass for Sane Web Development

Thanks!

Jeremy WeiskottenBlueleaf

@doctorzaius

QuickTime™ and a decompressor

are needed to see this picture.