好吧,我挖成萨斯文档,它看起来像有可能利用它们的功能做的,但现在看来似乎会是过于复杂,反正以后带来问题。
我发现做到这一点的最好办法是为当他们更新自己设置用户特定的模板。无论如何,这会更好地工作,因为请求在等待解析器时永远不会被延迟。
# unless cached_copy_exists
template = %Q{
@import '/path/to/color_scheme';
@import '/path/to/layout';
}
output = Sass::Engine.new(template, :syntax => :scss).render
# output rendered CSS to file for inclusion in HTML template
为了让自定义颜色,用户输入可以组装成SASS CSS变量的字符串和预先计划在模板文件传递给萨斯解析/渲染引擎。
更新:
每请求,下面是如何工作的,重点只是使用萨斯变量和预编码萨斯样式表(简化为隔离这一特定问题)更充实出例如:
# config/routes.rb
resources :stylesheets, only: [:show]
# app/controllers/stylesheets_controller.rb
class StylesheetsController < ApplicationController
layout nil
def show
styles = Stylesheet.find(params[:id])
base_stylesheet_path = Rails.root.join('app', 'assets', 'profile.scss')
# Build the string of SCSS we'll pass to the Sass rendering engine
@sass = <<-SASS
#{styles.to_sass}
@import "#{base_stylesheet_path}";
SASS
# Cache for long time
response.headers['Cache-Control'] = "public, max-age=#{1.year}"
respond_to do |format|
format.css
end
end
end
# app/views/stylesheets/show.css.erb
<%= raw Sass::Engine.new(@sass :syntax => :scss).render -%>
# app/models/stylesheet.rb
class Stylesheet < ActiveRecord::Base
serialize :variables, JSON
def to_sass
# Convert a hash of variables into SCSS
variables.each_pair.map do |name, value|
"$#{name}: #{value};"
end.join("\n")
end
end
# example for the stylesheet model
stylesheet = Stylesheet.new
stylesheet.variables[:primary_color] = "#0000ff"
stylesheet.save
你认为你能澄清你的脚步更清晰一点?小菜一碟! – 2012-03-26 11:04:38