使用在scss文件中的config.rb中定义的变量 [英] Use variable defined in config.rb in scss files

查看:446
本文介绍了使用在scss文件中的config.rb中定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在整个SCSS文件中,是否可以使用在指南针项目的config.rb文件中定义的变量?

解决方案

在您的config.rb文件中添加一个自定义模块:

 模块Sass :: Script :: Functions 
def custom_color(value)
rgb = options [:custom] [:custom_colors] [value.to_s] .scan(/?#)(..?)$/)。 first.map {| a | a.ljust(2,a).to_i(16)}
Sass :: Script :: Color.new(rgb)
end
end



然后设置您的变量(再次在config.rb文件中):

  sass_options = {:custom => {:custom_colors => {main=> #ff1122}}} 

然后在您的scss文件中,可以使用 custom_color() function:

  body {
background-color:custom_color主要);
}

您还可以编写另一个自定义函数,返回其他类型,测量等,通过传入字符串,然后返回适当的类实例。



有趣的是,这将允许你传递环境变量到罗盘命令行,



所以如果你的sass_options是:

  sass_options = {:custom => {:custom_colors => {main=> ENV [MAIN_COLOR]}}} 

您运行罗盘:

  MAIN_COLOR =#dd1122 bundle exec compass compile 


$ b b

然后,在命令行中传递的任何颜色将出现在结果css中。如果你使用Heroku,你可以 heroku config:set MAIN_COLOR =#224411 ,并且能够使用相同的scss文件在每个应用程序基础上设置模板颜色。


Is it possible to use a variable defined in the config.rb file of a compass project, throughout the SCSS files?

解决方案

In your config.rb file add a custom module:

module Sass::Script::Functions
  def custom_color(value)
    rgb = options[:custom][:custom_colors][value.to_s].scan(/^#?(..?)(..?)(..?)$/).first.map {|a| a.ljust(2, a).to_i(16)}
    Sass::Script::Color.new(rgb)
  end
end

And then set up your variables (again, in the config.rb file):

sass_options = {:custom => { :custom_colors => {"main" => "#ff1122"} } }

Then in your scss file, you can use the custom_color() function:

body {
  background-color: custom_color(main);
}

You could also write another custom function which returns other types such as font sizes, measurements, etc. by passing in strings, and then returning the appropriate class instance.

Interestingly, this would allow you to pass in environment variables into the compass command line, and that would generate different results.

So if you sass_options are:

sass_options = {:custom => { :custom_colors => {"main" => ENV["MAIN_COLOR"]} } }

And you run compass:

MAIN_COLOR=#dd1122 bundle exec compass compile

Then whatever color you pass in on the command line will appear in the resultant css. If you're using Heroku, you could heroku config:set MAIN_COLOR=#224411 and be able to set template colors on a per-app basis, using the same scss files.

这篇关于使用在scss文件中的config.rb中定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆