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]
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
[/ruby]

  • Exiting
    /Users/marcogodinez/Sites/diseweb/config/environment.rb:6:in `require’: /Users/marcogodinez/Sites/diseweb/lib/sass.rb:11: syntax error, unexpected tIDENTIFIER, expecting kDO or ‘{‘ or ‘(‘ (SyntaxError)
            :style => :nested,
         ^
    /Users/marcogodinez/Sites/diseweb/lib/sass.rb:12: syntax error, unexpected tIDENTIFIER, expecting kDO or ‘{‘ or ‘(‘
            :load_paths => [‘.’]
         ^
    /Users/marcogodinez/Sites/diseweb/lib/sass.rb:13: syntax error, unexpected ‘}’, expecting kEND
          }.merge! options
              ^
    /Users/marcogodinez/Sites/diseweb/lib/sass.rb:19: syntax error, unexpected kEND, expecting $end