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



有什么方法可以让SASS编译从数据库传递的数据吗?我可以使用DBI+ODBC从MSSQLServer2008获取数据,但如何使SASS编译传递的数据而不是文件?

没有发现任何有用的东西,更改sass核心文件听起来不是一个好主意。。。尤其是考虑到Ruby知识为零。

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

sass-gem目录中的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/sas/engine.rb中,我在末尾包含了这个文件:

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

它需要额外的dbidbd-odbc gem,并影响sass更新和监视。

希望这能帮助到别人。

最新更新