将 SASS 与用户指定的颜色结合使用

发布于 2024-10-10 23:03:22 字数 534 浏览 0 评论 0原文

我正在使用 Rails 3 构建一个网站,让用户拥有具有不同布局和配色方案的配置文件。我已经在使用 SASS,如果我能做这样的事情,变量将是无价的......

<link src="base_styles.css" rel="stylesheet">
<link src="color_schemes/users_choice.css" rel="stylesheet">
<link src="layouts/users_choice.css" rel="stylesheet">

其中配色方案定义将主要(全部?)SASS 变量指定在布局中使用的颜色。显然我不能像这样链接 SASS 或 CSS 文件,我需要将它们导入到 SASS 中。

如何在请求时将 SASS 文件动态导入解析器,然后缓存生成的 CSS 文件以供以后使用?

我考虑过在部署时构建每种可能的组合的丑陋路线,但是那如果我想让用户将来设置自己的颜色,仍然让我犹豫不决。 SASS 似乎是唾手可得的成果,不妨实施一下。

I'm building a website with Rails 3 that'll let users have profiles with different layouts and color schemes. I'm already using SASS, and the variables would be invaluable if I could do something like this…

<link src="base_styles.css" rel="stylesheet">
<link src="color_schemes/users_choice.css" rel="stylesheet">
<link src="layouts/users_choice.css" rel="stylesheet">

…where the color scheme definition would be primarily (entirely?) SASS variables specifying the colors to use in the layout. Obviously I can't just link the SASS or CSS files like this, I'll need to import them into SASS.

How can I import SASS files into the parser dynamically at request-time, then cache the resulting CSS files for use later?

I've considered going the ugly route of building every possible combination on deploy, but that still leaves me hanging if I want to let users set their own colors in the future. It seems like such low-hanging fruit with SASS that it might as well be implemented.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-10-17 23:03:22

好吧,我深入研究了 Sass 文档,看起来可以使用它们的函数,但看起来它会过于复杂,并且无论如何都会在以后引入问题。

我发现执行此操作的最佳方法是在用户更新设置时生成特定于用户的模板。无论如何,这效果更好,因为在等待解析器时请求永远不会延迟。

# 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 变量,并添加到传递给 Sass 解析/渲染引擎的模板文件之前。

更新:

根据请求,这里有一个更具体的示例,说明其工作原理,仅关注使用 Sass 变量和预编码的 Sass 样式表(经过简化以隔离此特定问题):

# 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   

Alright I dug into the Sass docs and it looks like it would be possible to do using their functions, but it seems like it'd be overly complicated and introduce problems later anyways.

The best way I have found to do this is to generate the user-specific template when they update their settings. This works better anyways, as a request is never delayed while waiting on the parser.

# 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

In order to allow custom colors, user-input could be assembled into SASS css variables in a string and prepended to the template file being passed to the Sass parsing/rendering engine.

Update:

Per request, here's a more fleshed-out example of how this works, focusing just on using Sass variables and a pre-coded Sass stylesheet (simplified to isolate this specific problem):

# 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   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文