有没有一种方法可以将application.scss渲染为CSS字符串?

时间:2018-10-07 16:14:43

标签: ruby-on-rails ruby

我正在尝试使用Roadie gem内嵌一些CSS模板(用于html到img,而不用于电子邮件),该模板需要一个CSS字符串。我想将application.scss文件转换为字符串,但是找不到可靠的方法。当前,我只是从浏览器中复制它,但我想以编程方式进行复制。

我尝试过的是:

str = File.read("app/assets/stylesheets/application.scss")
se = Sass::Engine.new(str, :syntax => :scss)
se.render 

这给我带来了由Bootstrap链轮之类的宝石加载的sass的问题:

Sass::SyntaxError: File to import not found or unreadable: bootstrap-variables

最好的方法是什么?

1 个答案:

答案 0 :(得分:2)

请发布您的from gensim.utils import lemmatize sentence = "The striped bats were hanging on their feet and ate best fishes" lemmatized_out = [wd.decode('utf-8').split('/')[0] for wd in lemmatize(sentence)] #> ['striped', 'bat', 'be', 'hang', 'foot', 'eat', 'best', 'fish'] ,但是该文件似乎正在尝试加载类似application.scss @import bootstrap-variables的文件,因此不会知道Sass::Engine文件是否不在与bootstrap-variables.scss相同的目录,尽管例如,如果使用的是rails bootstrap-sass gem,rails可能会出现。

请确保您的application.scss文件具有有效的sass并正确命名,并且与@import处于同一目录中,与可能导入application.scss的其他文件相同。

那么您应该能够做到:

application.scss

如果您尝试在rails内执行此操作,则很可能已通过gem加载了引导程序,并且rails将处理资产的预编译并知道从何处查找导入。不必尝试找出一切都满足您的目的,然后运行css = Sass::Engine.for_file('application.scss', {}).render 可能会更容易,然后您可以执行以下操作:

bundle exec rake assets:precompile

但是由于您已经进行了预编译,所以您可以这样做

filename = Dir.entries('public/assets/').find{|f| f.match(/application-\w+.css$/)}
css = Sass::Engine.for_file(filename, {}).render
#css is the flat css compiled from the rails precompile

神奇的css = File.read( Dir.entries('public/assets/').find do |filename| filename.match(/application-\w+.css$/) end ) 会生成一个平面的css文件,其名称类似于bundle exec rake assets:precompile

因此,我们在上面使用了一个正则表达式来对文件名进行模式匹配。

相关问题