Using Sass & Compass in Drupal Theming

69

description

In recent years, front end development tools have come a long way. Gone are the days of suffering through remembering multiple browser vendor prefixes. We can now focus on what matters most: Writing the code that makes our designs unique.Matt Farina and Nathan Smith will be presenting on how to use Compass/Sass in your Drupal workflow, significantly speeding up the processes of working with CSS. They will guide you past the mental hurdle of "not invented here" and explain how Ruby and PHP can be the best of friends. Or rather, how using Sass does not require much Ruby knowledge at all.Outline:— What is Sass? Compass? (and SCSS too)— Installing Sass & Compass— Compass config in a theme— Sass basics— Compass CSS3 Hotness— More Miscellaneous HotnessYou can download the PDF slides here:— http://j.mp/drupal-compass

Transcript of Using Sass & Compass in Drupal Theming

Page 1: Using Sass & Compass in Drupal Theming
Page 2: Using Sass & Compass in Drupal Theming

Relax. Don’t fret over taking notes.

Download the presentation slides here:

http://j.mp/drupal-compass

Note: It’s okay if you read ahead. We don’t mind. :)

Page 3: Using Sass & Compass in Drupal Theming

Design & User Experience

Using Sass & Compassin Drupal Theming

Nathan Smith & Matt Farina

Page 4: Using Sass & Compass in Drupal Theming

Hello :)We like Drupal, Sass & Compass.We hope you do too, or will soon.

Let us introduce ourselves...

Page 5: Using Sass & Compass in Drupal Theming

Nathan Smith isa UX designer at

Matt Farina is aweb developer atHP Cloud Services

http://projekt202.com

http://hpcloud.com

yay, books!

Note: We co-authored these books, respectively. We’re not taking all the credit.

Page 6: Using Sass & Compass in Drupal Theming

Sass & Compass are made possible by...

http://hamptoncatlin.com

He is no longer activein working on Sass.

http://nex-3.com

Still works on Sass,in his “20% time.”

http://chriseppstein.github.com

Also, member ofSass core team.

Nathan WeizenbaumGoogle Chrome (Dart)

Hampton Catlincreated Haml & Sass

Chris Eppsteincreated Compass

...and other community contributors.

Page 7: Using Sass & Compass in Drupal Theming

So, that just happened:

(anyways...)

Page 8: Using Sass & Compass in Drupal Theming

Part 1:Why Sass?

Page 9: Using Sass & Compass in Drupal Theming

“The backdrop to this debate is that CSS is absolutelythe worst, least productive part of the web platform.Apps teams at Google are fond of citing the memethat ‘CSS is good for documents, but not for apps.’I push back on this, noting the myriad of ways inwhich CSS is abysmal for documents [as well].”

On -webkit vendor prefix drama

http://infrequently.org/2012/02/misdirection

Alex Russell @slightlylateGoogle Chrome team / Dojo Toolkit co-creator

Page 10: Using Sass & Compass in Drupal Theming

So what’s to be done about CSS?

The answer has actually been staring us in the facefor awhile now, but it’s only been in recent years thatCSS has become a first-class citizen. CSS is nowbeing approached with an architectural mindset.

PHP is a (recursive) acronym for “PHP: HypertextPreprocessor.” So, preprocessing is a considered aGood Thing™ for HTML. How does it work for CSS?

Page 11: Using Sass & Compass in Drupal Theming

http://thingsorganizedneatly.tumblr.com/post/9494864300/submission-the-compulsively-tidy-ursus-wehrli

CSS served to browserNeatly organized *.sass

Page 12: Using Sass & Compass in Drupal Theming

“Years ago, we wrote static HTML. Now, wealmost never do. Our HTML is virtually alwayspreprocessed, be it by PHP, Python, Ruby,Perl, .NET, Java, or something else. Today,we almost never write and serve straightHTML.”

Jeff Croft @jcroftChief “Designerd” at nGen Works

http://jeffcroft.com/blog/2012/jan/31/on-css-preprocessors

Page 13: Using Sass & Compass in Drupal Theming

Noteworthy CSS preprocessors

LESShttp://lesscss.org

Sass & Compasshttp://sass-lang.comhttp://compass-style.org

Stylus & Nibhttp://learnboost.github.com/stylushttp://visionmedia.github.com/nib

Note: Obviously, today we will only be covering Sass and Compass.

Page 14: Using Sass & Compass in Drupal Theming

CSS Sass Compasshttp://sonspring.com/journal/sass-for-designers

Page 15: Using Sass & Compass in Drupal Theming
Page 16: Using Sass & Compass in Drupal Theming

Compass extends the functionality ofSass by offering a plugin architecture.It also handles vendor prefixes like achamp. Today, when you hear us say“Sass,” we may blur the lines betweenCompass and Sass. Hope that’s okay.

Compass is essentially Sass’extra-awesome secret-sauce.

Page 17: Using Sass & Compass in Drupal Theming

Oversimplification, FTW!

Compass is to Sassas

jQuery is to JavaScript

Page 18: Using Sass & Compass in Drupal Theming

Sass & SCSS are functionally equivalent

We prefer using Sass instead ofSCSS because hey, less typing!

Page 19: Using Sass & Compass in Drupal Theming

Anti-pattern of nested selectors in Sass

Page 20: Using Sass & Compass in Drupal Theming

Compass makes vendor prefixes easy...

Page 21: Using Sass & Compass in Drupal Theming

Compass brings sanity to gradients...

Page 22: Using Sass & Compass in Drupal Theming

Text editors and IDE’s that support Sass/SCSS syntax

Aptanahttp://aptana.org

BBEdithttp://barebones.com/bbedit

Chocolathttp://chocolatapp.com

Codahttp://panic.com/coda

E Text Editorhttp://e-texteditor.com

Eclipsehttp://eclipse.org

Emacshttp://gnu.org/software/emacs

Espressohttp://macrabbit.com/espresso

GEdithttp://projects.gnome.org/gedit

Komodohttp://activestate.com/komodo-ide

Netbeanshttp://netbeans.org

PhpStormhttp://jetbrains.com/phpstorm

PyCharmhttp://jetbrains.com/pycharm

RubyMinehttp://jetbrains.com/ruby

SubEthaEdithttp://codingmonkeys.de/subethaedit

Sublime Texthttp://sublimetext.com/dev

TextMatehttp://macromates.com

Vimhttp://vim.org

Visual Studiohttp://microsoft.com/visualstudio

http://sass-lang.com/editors.html

^We recommend

Page 23: Using Sass & Compass in Drupal Theming

GUI to compile Sass & Compass

Compass.apphttp://compass.handlino.com

Scouthttp://mhs.github.com/scout-app

CodeKithttp://incident57.com/codekit

Note: Personally, we prefer to run command-line “compass watch”

Page 24: Using Sass & Compass in Drupal Theming

Impression of Sass...

Great for CSS, in Ruby on Rails projects.

Page 25: Using Sass & Compass in Drupal Theming

Great for CSS, in Ruby on Rails projects.

Great for CSS, in Ruby projects.

Impression of Sass...

Page 26: Using Sass & Compass in Drupal Theming

Great for CSS, in Ruby on Rails projects.

Great for CSS, in Ruby projects.

Great for CSS, in any project!

Impression of Sass...

Page 27: Using Sass & Compass in Drupal Theming

What if I want to use Sass for somethingthat wasn’t written in Ruby... impossible?

But, wait...

Page 28: Using Sass & Compass in Drupal Theming

When I think about not having Sass...

http://en.wikipedia.org/wiki/The_Incredible_Hulk_(TV_series)

Page 29: Using Sass & Compass in Drupal Theming

Yes, but the amount of Rubyknowledge it takes to use Sass isbasically zero.

Nathan is not a hardcore programmer,but he manages to use Sass just fine.

Matt explained to Nathan how to use*.sass files in Drupal. Matt is the man.

So, does Sass require Ruby?

Page 30: Using Sass & Compass in Drupal Theming

Part 2:Whither amodule?

Page 31: Using Sass & Compass in Drupal Theming

Why use a Drupal Module?

Possibly Bad Reasons...— You don’t want to install Sass/Compass gems.— You want Drupal to do everything, end-to-end.

Possibly Good Reasons...— To use base-theme mixins/variables in sub-theme.— So that modules can supply mixins/variables.

Page 32: Using Sass & Compass in Drupal Theming

Sass/SCSS modulehttp://drupal.org/project/sass

Depends on “phamlp”http://code.google.com/p/phamlpLast updated in September 2010.

PHP needs code write access to themeThis can lead to bad security practices.

Despite the name, only handles SCSS files#sadpanda

Page 33: Using Sass & Compass in Drupal Theming

Compass stylesheet tool modulehttp://drupal.org/project/compass

Ruby (Compass) does the heavy liftingThis means you need Ruby and PHP on your server.

Can compile CSS using devel or cronThis is similar to “compass compile” but using Drupal.

Compass location across environmentsCompass location set it config. Needs to be managed.

Version and permissions can be an issue

Module admin page is hidden (a bug)

••

Page 34: Using Sass & Compass in Drupal Theming

Sassy modulehttp://drupal.org/project/sassy

Several dependencies http://github.com/richthegeek/phpsass — and three other modules.

Hooks for namespaces/implementationsIncomplete (PHP) implementation of Compass, Bootstrap, Foundation.

Sass & SCSS files in .info file styles[]

Stores generated files in public files

Page 35: Using Sass & Compass in Drupal Theming

Jeff Croft, quoted previously, programs in Python anduses Django. He also makes use of Sass, via Ruby.

We advocate trying to move beyond a “not inventedhere” mindset, and adopting a more hybrid workflow.

Just as it wouldn’t make much sense to port Drupalout of PHP, we think it’s most logical to use first-partyreleases of Sass and Compass directly, if possible.

TL;DR — Stick “close to the metal” if you want to stay on the cutting-edge.

“Best tool for the job”

Page 36: Using Sass & Compass in Drupal Theming

ACHIEVEMENT UNLOCKED!Presuppositions Challenged

Note: I don’t think this photo is necessarily relevant, but Ithink we can all agree that raw electricity looks awesome.

https://en.wikipedia.org/wiki/The_Prestige_(film)

Page 37: Using Sass & Compass in Drupal Theming

Part 3:How to use

Page 38: Using Sass & Compass in Drupal Theming

For the sake of brevity, we are going to assume inthis talk that your environment already has Ruby setup. If not, then follow the instructions here...

http://sonspring.com/journal/sass-for-designers

Prerequisite: Ruby

Page 39: Using Sass & Compass in Drupal Theming

Setting up Compass & Sass

gem install compass

From the command-line (Terminal.app, iTerm, etc)...

This will also automatically download the Sass gem, adependency of Compass. There’s no need to installseparately. They’re typically used in tandem anyway.

Page 40: Using Sass & Compass in Drupal Theming

Take a hybrid PHP + Ruby approach

Page 41: Using Sass & Compass in Drupal Theming

Add config.rb to your theme directory

Page 42: Using Sass & Compass in Drupal Theming

“cd” to your theme directory,then run “compass watch”

If you just want to flush cache and compile, run“compass clean && compass compile”

Page 43: Using Sass & Compass in Drupal Theming

application.css is compiledand (optionally) minifiedvia “compass watch”

Serving as a aggregate,application.sass is builtfrom “_file.sass” partials.

I bring over what I do likefrom default Drupal CSSas SCSS files, to import:

_search.scss_system.base.scss_system.theme.scss

Compass outputs theresulting compiled fileas “application.css”

Sass partials begin with anunderscore, aren’t compiledto *.css files individually.

Page 44: Using Sass & Compass in Drupal Theming

Theme *.info file – before Sassstylesheets[all][] = assets/css/reset.css

stylesheets[all][] = assets/css/override/system.base.css

stylesheets[all][] = assets/css/override/system.theme.css

stylesheets[all][] = assets/css/override/search.css

stylesheets[all][] = assets/css/960_12_col.css

stylesheets[all][] = assets/css/text.css

stylesheets[all][] = assets/css/formalize.css

stylesheets[all][] = assets/css/site.css

; KILL THESE DEFAULT STYLESHEETS:

stylesheets[all][] = ctools.css

stylesheets[all][] = field.css

stylesheets[all][] = node.css

stylesheets[all][] = system.messages.css

stylesheets[all][] = system.menus.css

stylesheets[all][] = user.css

stylesheets[all][] = views.css

For brevity, only CSS files shown

Files in orange arelisted to prevent

Drupal from adding‘em automatically.

Page 45: Using Sass & Compass in Drupal Theming

Theme *.info file – before Sass

stylesheets[all][] = assets/css/reset.css

stylesheets[all][] = assets/css/override/system.base.css

stylesheets[all][] = assets/css/override/system.theme.css

stylesheets[all][] = assets/css/override/search.css

stylesheets[all][] = assets/css/960_12_col.css

stylesheets[all][] = assets/css/text.css

stylesheets[all][] = assets/css/formalize.css

stylesheets[all][] = assets/css/site.css

For brevity, only CSS files shown

Minus the nonexistent files, to ward off Drupal defaults...

This compresses down to a single CSS file,in the Performance cache setting in Drupal.

Page 46: Using Sass & Compass in Drupal Theming

stylesheets[all][] = assets/css/application.css

; KILL THESE DEFAULT STYLESHEETS:

stylesheets[all][] = ctools.css

stylesheets[all][] = field.css

stylesheets[all][] = node.css

stylesheets[all][] = search.css

stylesheets[all][] = system.base.css

stylesheets[all][] = system.theme.css

stylesheets[all][] = system.menus.css

stylesheets[all][] = system.messages.css

stylesheets[all][] = user.css

stylesheets[all][] = views.css

Theme *.info file – after Sass

For brevity, only CSS files shown

Files in orange arelisted to prevent

Drupal from adding‘em automatically.

Page 47: Using Sass & Compass in Drupal Theming

stylesheets[all][] = assets/css/application.css

Theme *.info file – after Sass

ACHIEVEMENT UNLOCKEDOnly one CSS HTTP request!

With (or without) cached CSScompression in Performance.

Minus the nonexistent files, to ward off Drupal defaults...

Page 48: Using Sass & Compass in Drupal Theming

This is still worthdoing. I’ll explainwhy in a second.

Page 49: Using Sass & Compass in Drupal Theming

Even if using “compass watch” with:

output_style = :compressed

You should still use Drupal’s CSScompression under Performance.

Why? Well, let’s flip to the next slide...

Page 50: Using Sass & Compass in Drupal Theming

Benefits of using Drupal’s CSScompression, not just Compass:

1. Cache-busting. File name is changed to... “css_VvtjvgJd9tI4sfrbHdy7Yvyc5fm9jVFoHRnrEBsJlQs.css”

2. <link /> instead of <style>@import</style> (older IE versions are slower at the latter)

3. Drupal replaces url(../images/file.png) with url(/full/path/to/images/file.png) — Useful if you have assets on a CDN or subdomain.

Page 51: Using Sass & Compass in Drupal Theming

Compass output, before Drupal compression

Page 52: Using Sass & Compass in Drupal Theming

Compass output, after Drupal compression

Page 53: Using Sass & Compass in Drupal Theming

Achievement Unlocked!You have now attained CSS

bliss.

Don’t forget to rate this as the besttalk you’ve ever heard in your life.

Or, maybe we forgot something...

Page 54: Using Sass & Compass in Drupal Theming

Stop the presses!What about debugging?

How do I see what stylesare coming from which

*_file.sass partial?

Page 55: Using Sass & Compass in Drupal Theming

Part 4:Debugging& Demos

Page 56: Using Sass & Compass in Drupal Theming
Page 57: Using Sass & Compass in Drupal Theming

Cryptic errormessages arenever helpful.

Page 58: Using Sass & Compass in Drupal Theming

^ I believe debugging this way stinks.

Page 59: Using Sass & Compass in Drupal Theming

When things don’t go as planned...

http://autos.yahoo.com/blogs/motoramic/porsche-stuck-wet-cement-proves-karma-exists-210302568.html

Page 60: Using Sass & Compass in Drupal Theming

Just a one-line change in config.rb

Page 61: Using Sass & Compass in Drupal Theming

Emits *.sass “stack trace” for each style

Page 62: Using Sass & Compass in Drupal Theming

A quick tip on source control

While you can track your *.css files viasource control, the ones you really careabout are your *.sass files, which are thecanonical “source of truth.” Educate peerdevelopers not to edit *.css, but insteadtweak *.sass files when making changes.

Page 63: Using Sass & Compass in Drupal Theming

Now time for somedemonstrations :)

Note: If you’re viewing these slides, but missed oursession, we showed “compass watch” in action.

Page 64: Using Sass & Compass in Drupal Theming

“Luck, be a lady...” (live demo = dice roll)

http://kloucksphoto.blogspot.com/2011/06/guys-and-dolls-witty-wry-wonderful.html

Page 65: Using Sass & Compass in Drupal Theming

If you want to tinker with Haml & Sass...

Page 66: Using Sass & Compass in Drupal Theming

“We keep thinking we can write better CSS if we justtry harder, that the next site will be clean and staythat way. [Her] presentation shows that in fact, messyCSS is the direct result of our worst best-practices.We need to re-examine those assumptions with aneye to practicality and scalability — as well asaccessibility, standards, and fabulous design.”

Nicole Sullivan @stubbornellaCSS Ninja & Front-end Performance Engineer

http://stubbornella.org/content/2011/04/28/our-best-practices-are-killing-us

(Nicole helped make Yahoo & Facebook more efficient)

Page 67: Using Sass & Compass in Drupal Theming

Feel free to say hi on Twitter, and/or check out our personal sites for more of our work.

http://twitter.com/NathanSmithPersonal site — http://sonspring.com

http://twitter.com/MattFarinaPersonal site — http://mattfarina.com

Thanks for coming. Keep in touch!

Page 68: Using Sass & Compass in Drupal Theming

Questions? We (might) have answers.

Feel free to share the link with friends, enemies, etc.

Download the presentation slides here:

http://j.mp/drupal-compass

Page 69: Using Sass & Compass in Drupal Theming

What did you think?Locate this session on theDrupalCon Denver website

http://denver2012.drupal.org/program

Click the “Take the Survey” link.

Thank You!