从数据库获取SASS(编译传递的数据而不是从文件中读取)

有没有办法让SASS编译数据从数据库传递? 我可以使用DBI + ODBC从MSSQL Server 2008获取数据,但是如何使SASS编译传递数据而不是文件?

没有找到任何有用的东西,改变sass核心文件听起来不是一个好主意…特别是考虑零Ruby知识。

我最终使用了这段代码。 完全不优雅,但我希望它能帮助那些有变态需求的人。

sass gem dir中的sql.rb:

require 'dbi' require 'sass' require config with database credentials if File.exist?(config) require config SQL_OPTIONS = { :style => :nested, :load_paths => ['.'], :cache => true, :cache_location => './.sass-cache', :syntax => :scss, :filesystem_importer => Sass::Importers::Filesystem, :css_location => "./public/stylesheets", :always_update => true, :template_location => [["scss", "css"]] }.freeze db = DBI.connect('dbi:ODBC:driver={SQL Server};server=' + $db_host, $db_user, $db_pass) db.select_all('SELECT name, scope, content FROM ' + $db_name + '.dbo.scss') do | row | File.open(Dir.pwd + '/css/' + row['name'] + '.css', 'w') do |css| css.puts Sass::Engine.new(row['content'], SQL_OPTIONS).render puts ' overwrite css/' + row['name'] + '.css [db]' end end else puts 'ignore database stylesheets, config_ruby doesn\'t exist' end 

在lib / sass / engine.rb中,我在最后包含了这个文件:

 require File.expand_path(File.dirname(__FILE__) + '../../../sql') 

它需要额外的dbidbd-odbcgem并影响sass –update以及–watch。

希望这有助于某人。