I don\'t have much experience with Ruby. I want to @import sass from database instead of filesystem. I did not find any examples online. How should I go about implementing t
Since you're using Compass to compile, you can add a custom Sass importers in the Compass config file. For example, compiling using compass compile -c config.rb
, you would include something like this in your config.rb
file:
require File.join(File.dirname(__FILE__), 'importer.rb')
Sass.load_paths << Sass::Importers::Custom.new()
Then in importer.rb
in the same directory, you would include your importer definition:
module Sass
module Importers
class Custom < Base
def find(name, options)
if name == '[globals]'
options[:syntax] = :scss
options[:filename] = 'globals'
options[:importer] = self
return Sass::Engine.new("$imported-variable: blue;", options)
else
return nil
end
end
def find_relative(uri, base, options)
nil
end
def key(uri, options)
[self.class.name + ":" + uri, uri]
end
def mtime(uri, options)
nil
end
def to_s
'[custom]'
end
end
end
end
Then in your Sass file you can use the importer:
@import '[globals]';
p {
color: $imported-variable;
}
Of course, this is just a dummy implementation that only accepts a URI matching "[globals]"
. You'll need to supply your own implementation that accesses your MySQL database, as I don't have any experience with database access in Ruby. Hopefully this should get you a little closer, though, in addition to the links that @Sean has provided.
Out of the box, sass will only import local files (anything else is compiled into a CSS @import statement) but as the sass docs explain you can write your own custom importers by extending the Sass::Importers::Base class.
This answer to another question gives an example of a custom HTTP importer. You could approach this in (at least) two ways: write a custom importer for your database or serve the sass file over HTTP from the database (via a simple PHP page or something)