Skip to content

Maintaining Sass constants in one file… the easy, hacky way

by Topper on April 18th, 2008

If you're like us and you want to have multiple separated sass files (where the resulting css gets bundled at build time) but want to maintain constants for use across the whole site in one other file, this monkey patch is for you.

Just add a file to your /lib directory (for rails) and require it in environment.rb

Then in your main sass directory add a constants.sass and define constants per normal... they will then be available to all your other sass files.

RUBY:
  1. module Sass
  2.   class Engine
  3.    
  4.     SASS_CONSTANTS_PATH = "#{Sass::Plugin.options[:template_location]}/constants.sass"
  5.    
  6.     def initialize(template, options={})
  7.       if File.exists?(SASS_CONSTANTS_PATH)
  8.         template = File.read(SASS_CONSTANTS_PATH) + "\n" + template
  9.       end
  10.       @options = {
  11.         :style => :nested,
  12.         :load_paths => ['.']
  13.       }.merge! options
  14.       @template = template.split(/\n?\r|\r?\n/)
  15.       @lines = []
  16.       @constants = {"important" => "!important"}
  17.     end
  18.   end
  19. end

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS