【问题标题】:Get SASS from database (compile passed data instead of reading from file)从数据库获取 SASS(编译传递的数据而不是从文件中读取)
【发布时间】:2012-07-20 22:10:30
【问题描述】:

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

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

【问题讨论】:

    标签: ruby sql-server sass


    【解决方案1】:

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

    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/sass/engine.rb 我最后包含了这个文件:

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

    它需要额外的 dbidbd-odbc gems,并影响 sass --update 和 --watch。

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-01-20
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 2021-09-30
      相关资源
      最近更新 更多