Getting started with SASS and HAML in Windows – a front end developers guide.

I am one lazy lady – so when I heard there was a way I type less stuff in my day to day job I jumped at the chance. I realise I’m a bit late on the SASS/HAML train, but baby – that’s how I roll.

SASS & HAML are kind of what jQuery is to Javascript. SASS being a framework for CSS and HAML – XHTML. We are only going to be using these tecnologies to create plain ol’ XHTML & CSS so we don’t need to worry about getting all Rubied up (not to a great extent anyway)

Requirements (download & install these)

  1. RUBY
  2. SASS & HAML (we’ll install this in a bit)

All installed? Sweet.

Getting started with SASS

Of course the SASS tutorial is an excellent place to start, however not being a Ruby developer it took me a little while to figure out how it all fits together in Windows – here’s the lowdown:

  1. You will be commonly entering commands in the ‘Start command prompt with Ruby’ application located wherever Ruby was installed on your PC – so run that now and input
    gem install haml

    Thanks to Mikkel Ricky below for letting me Know Sass & HAML have now been split so you’ll also need:

    gem install sass

    this will enable the SASS & HAML parsing functionality

  2. SASS has a handy function that will watch an entire folder for changes to .scss files and automatically create .css files.
    sass --watch stylesheets/sass:stylesheets/compiled

    A more Windowsy example:

    sass --watch "D:/Docs/dropbox/web/lr/scss":"D:/Docs/dropbox/web/lr/css"

    This command will watch the folder D:\web\example-site\scss for changes to any .scss files and automatically create a .css file with the same name in D:\web\example-site\css

  3. I went one step further and created a batch file that starts the Ruby command prompt and then automatically runs the watch command. Well – I say created I just copied the existing Ruby command prompt batch and added a load of watch commands to the end.
    @ECHO OFF
    REM Determine where is RUBY_BIN (where this script is)
    PUSHD %~dp0.
    SET RUBY_BIN=%CD%
    POPD
    
    REM Add RUBY_BIN to the PATH
    REM RUBY_BIN takes higher priority to avoid other tools
    REM conflict with our own (mainly the DevKit)
    SET PATH=%RUBY_BIN%;%PATH%
    SET RUBY_BIN=
    
    REM Display Ruby version
    ruby.exe -v
    
    REM Run SASS
    sass --watch "D:/Docs/dropbox/web/lr/scss":"D:/Docs/dropbox/web/lr/css"

    Download this script
    Just put the above code in a text file, edit the sass lines, and save it as whatever.bat somewhere nice.

    Thanks to commenter Jason for fixing my SASS folder watching code :)

Now it's time to learn the language a bit. After you have done that, come back here and check out my SASS mixins - you'll probably know what mixins are by then as well.

Getting started with HAML

HAML was a bit of a bugger to get going, but luckily you can learn by my mistakes. For some reason HAML doesn't have a built in watcher command. I spent ages trying to find a Ruby script that would work and the closest I found is this one by Blake Smith. It seemed to screw up windows folders but after a bit of trial and error I came up with this:

HAML watcher for Windows

# Script to watch a directory for any changes to a haml file
# and compile it.
#
# USAGE: ruby haml_watch.rb
#
require 'rubygems'
require 'fssm'
 
directory = ARGV.first
FSSM.monitor(directory, '**/*.haml') do
  update do |base, relative|
    input = "#{relative}"
    output = "#{relative.gsub!('.haml', '.html')}"
    command = "haml #{input} #{output}"
    %x{#{command}}
    puts "Regenerated #{input} to #{output}"
  end
end

Download this script
You'll need to install the 'fssm' gem from the Ruby command prompt:

gem install fssm

Example: (run from Ruby command prompt)

haml_watch.rb d:\web\site1

Also, make sure none of your site folder names have spaces in them! I learnt this the hard way as I keep all my sites in my Dropbox folder which is brilliantly named 'My Dropbox' It wasn't even that easy to rename! Had to use pyDropboxPath.

Forcing HAML to use double quotes

Fix found here Which basically amounts to text-editing engine.rb which I found at C:\Ruby192\lib\ruby\gems\1.9.1\gems\haml-3.0.18\lib\haml and changing the line:

:attr_wrapper => "'",

to

:attr_wrapper => "\"",

Including HAML in HAML

If anyone has a neater way of doing this please email me. Please note your path will be relative to the page you are including from.

= ::Haml::Engine.new(File.read(File.dirname(__FILE__) + '/pathto/file.haml'), instance_variable_get(:@haml_buffer).options).render

Note that HAML will automatically close tags so you can't have half tags in say a header include. EG if you want your header to be

!!!
%html
    %head
        %meta{'http-equiv' => 'Content-Type', :content => 'text/html; charset=UTF-8'}
        %title Lutyens & Rubinstein   
        %link{:rel => 'stylesheet', :type => 'text/css', :href => '/css/meyer-reset.css'}
        %link{:rel => 'stylesheet', :type => 'text/css', :href => '/css/main.css'}
 
    %body
        %div#wrapper
            %div#content

That ain't gonna work as html, body, #wrapper, #content will all be closed off before you include it. Don't worry though as you can mix HAML with plain HTML like so:

!!!
<html>
    %head
        %meta{'http-equiv' => 'Content-Type', :content => 'text/html; charset=UTF-8'}
        %title Lutyens &amp; Rubinstein   
        %link{:rel => 'stylesheet', :type => 'text/css', :href => '/css/meyer-reset.css'}
        %link{:rel => 'stylesheet', :type => 'text/css', :href => '/css/main.css'}
 
    <body>
        <div id="wrapper">
            <div id="content">

bare in mind that HAML seems to cache its includes - I am looking into how to get around this.

This entry was posted in haml, sass and tagged , . Bookmark the permalink.

9 Responses to Getting started with SASS and HAML in Windows – a front end developers guide.

  1. monn says:

    Running into the same issues you’re having with folder watching in windows. It just doesn’t update like you said. I’ve just resorted to watching individual files for now.

    I’d be surprised if we’re the only 2 to come up against this, but im not finding much online. I also tried the current alpha version 3.1.0.alpha.50 – sadly folder watching still isnt working.

    • admin says:

      Yes I agree. it was working for me initially – but then stopped without reason. I have to admit this was more of an experiment for me rather than something I use in the real world so I have not looked into it that much. Do let me know if you get it working!

  2. Ash says:

    Thanks for this tutorial. It was very helpful. Especially the batch file for SASS compiling. I’m wondering though if there’s some way to avoid having to change the paths depending on what project I’m working on.

    Do you think it would be possible to setup the script so it watched all folders in say “d:\web” and then when a change is detected save the .css file relative to the .scss file?

    So when I change “d:\web\site1\css\styles.scss” it compiles to “d:\web\site1\ccs\styles.css”? Then later on if I’m working on “d:\web\site2″ I won’t have to change the script?

    • admin says:

      PS sorry for the lateness of my reply, been away! FThe only way I can think off the top of my head would be to use scripting to detect your site root and build the path that way. EG in PHP:

      $root = $_SERVER['DOCUMENT_ROOT'];
      $sassRoot = $root.”/scss”;

  3. Travis Tilley says:

    I’ve recently fixed a few windows oddities that popped up after the rubinius fix but I’m not using windows for anything remotely ruby so I certainly could have missed something (or a few somethings).

    If anyone here is still having problems, please let me know on the github issue tracker for FSSM!

  4. Found out what you have to do to make the watch directory issue work. Instead of:


    sass --watch "D:\Docs\dropbox\web\lr\scss":"D:\Docs\dropbox\web\lr\css"

    You have to use the UNIX directory structure:


    sass --watch "D:/Docs/dropbox/web/lr/scss":"D:/Docs/dropbox/web/lr/css"

    Works like a charm.

  5. Mikkel Ricky says:

    Very nice tutorial. I’ve used it to successfully introduce a colleague of mine to Sass.

    Note, however, that Haml and Sass have been split into independent projects [http://nex-3.com/posts/103-haml-and-sass-are-split], and therefore you need to

    gem install sass
    

    in order to install sass.

  6. Pingback: Installing Ruby, Rails, Sass on Windows - Regan Y .com

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">