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

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:
module Sass
  class Engine
   
    SASS_CONSTANTS_PATH = "#{Sass::Plugin.options[:template_location]}/constants.sass"
   
    def initialize(template, options={})
      if File.exists?(SASS_CONSTANTS_PATH)
        template = File.read(SASS_CONSTANTS_PATH) + "\n" + template
      end
      @options = {
        :style => :nested,
        :load_paths => ['.']
      }.merge! options
      @template = template.split(/\n?\r|\r?\n/)
      @lines = []
      @constants = {"important" => "!important"}
    end
  end
end

No Comments Yet

You can be the first to comment!

Speak Your Peace

  • Comment Policy:Could go here if there's a nagging need Login Instructions: Would go here if there's a desire.