从数据库中获取 SASS(编译传递的数据而不是从文件中读取) [英] Get SASS from database (compile passed data instead of reading from file)
问题描述
有没有办法让SASS编译从数据库传过来的数据?我可以使用 DBI + ODBC 从 MSSQL Server 2008 获取数据,但是如何让 SASS 编译传递的数据而不是文件?
Is there any way to make SASS compile data passed from database? I can use DBI + ODBC to get data from MSSQL Server 2008, but how do I make SASS compile passed data instead of file?
没有发现任何有用的东西,而且更改 sass 核心文件听起来不是一个好主意……尤其是考虑到零 Ruby 知识.
Didn't find anything useful, and changing sass core files doesn't sound like a best idea... Especially considering zero Ruby knowledge.
推荐答案
我最终使用了这段代码.完全不优雅,但我希望它能帮助那些有变态需求的人.
I ended up using this chunk of code. Totally not elegant, but I hope it will help someone with pervertish needs.
sass gem 目录中的 sql.rb:
sql.rb in sass gem dir:
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 中,我在最后包含了这个文件:
In lib/sass/engine.rb I included this file at the end:
require File.expand_path(File.dirname(__FILE__) + '../../../sql')
它需要额外的 dbi
和 dbd-odbc
gems 并影响 sass --update 和 --watch.
It requires additional dbi
and dbd-odbc
gems and affects sass --update as well as --watch.
希望这对某人有所帮助.
Hope this helps someone.
这篇关于从数据库中获取 SASS(编译传递的数据而不是从文件中读取)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!