在一个单独的控制器动作做到这一点:
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
然后,您可以包括雇员再培训局的变量中模板。请注意,这将通过每个负载上的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")
Webpack将不会通过ERB解释器运行资产 - 链轮然而。您还需要了解资产通常是在部署时编译的,因此任何变量都不能以请求为基础。更好的解决方案可能是使用HTML中的类在样式表中设置不同的规则以供使用。 – max