I am working on a Rails 3.1 app and am happily using SASS and CoffeeScript. I particularly like the SASS extensions of variables and imports. I have constructed a single _global-settings.css.scss file which contains ALL of the hex constant values I use throughout all of my stylesheets. This gives me a single place to manage colors, fonts and layout dimensions. Very DRY.
But, if I wish to use JQuery to tweak my css dynamically, I no longer have access to my css compile-time variables, and must reproduce the same data as a JSON hash in a .js.coffee file. Not DRY at all.
Here is my question: Before I go off and build a rake task to munge my css settings file into an equivalent CoffeeScript hash, does anyone have a more clever idea? Like hiding all the values in a hidden div in an html file?
You’d have an easier time moving your CSS configuration into Ruby and then sending your
_global-settings.css.scssand a small piece of CoffeeScript through ERB. Then you have your settings in place and you can access them everywhere.Somewhere in Ruby you’d have this:
Then rename your
_global-settings.css.scssto_global-settings.css.scss.erband use things like this inside it:And inside a
global_settings.js.coffee.erbyou could have this:You could even write a simple view helper that would SASSify a Hash:
and then you could say this in your
_global-settings.css.scss.erb:You could also monkey patch a
to_sassinto Hash and useCSS_SETTINGS.to_sassbut that’s probably taking things one step too far.