在单独的控制器操作中执行此操作:
layout.html.erb
<%= stylesheet_link_tag stylesheet_path() %>
样式表显示控制器动作
# Render the template to a string
css = Sass::Engine.new(
render_to_string("path/to/erb_template", :layout => false),
syntax: :scss,
cache: false,
read_cache: false,
style: :standard,
sprockets: {
context: self.view_context,
}
).render
# respond with the rendered string
respond_to do |format|
format.css { render plain: css, :content_type => "text/css" }
end
然后您可以在模板中包含 ERB 变量。请注意,这将在每次加载时通过 sass 引擎,因此您需要在生产环境中缓存它。
如果您想将所有 sass 很好地组织到单独的文件中,只需将变量放在一个文件中以呈现为 ERB,其余的则作为文本文件读入。
rendered_sass = [render_to_string("stylesheets/colours", :layout => false),
File.read(Rails.root.join("app", "javascript", "stylesheets", "main.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "base.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "reset.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "type.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "layout.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "map.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "audio-player.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "slider.scss"))].join("\n\r")