且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从数据库而不是文件系统导入SASS文件

更新时间:2023-01-22 11:48:55

由于使用的是Compass进行编译,因此可以在Compass配置文件中添加自定义Sass导入器。例如,使用 compass compile -c config.rb 进行编译,您将在 config.rb 中包含类似内容。文件:

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()

然后在进口商中使用。 rb 在同一目录中,您将包括进口商定义:

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

然后在您的Sass文件中,您可以使用导入程序:

Then in your Sass file you can use the importer:

@import '[globals]';
p {
    color: $imported-variable;
}

当然,这只是一个虚拟实现,仅接受与 [全局变量] 。您需要提供自己的访问MySQL数据库的实现,因为我没有使用Ruby进行数据库访问的经验。希望除了@Sean提供的链接之外,它还可以使您更加接近。

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.