在config.rb中为scss文件创建scss变量 [英] Creating scss variable in config.rb for scss files
问题描述
如何在Config.rb中为SCSS文件定义一个SCSS变量[COMPASS项目]
我的用例
在Config.rb文件中类似
a = true
在style.scss中,我喜欢使用
<$ p这样的变量$ p>
@if a == true {
背景:#fff;
}
@else {
背景:#000;
}
解决方案之一
http://worldhousefinder.com/?p=124141
但不是为我工作。
您不能/不应该这样做。您将对资产进行预编译,因此无法动态适应变化的变量。这可能对您的config.rb变量有效,但是使用它是一种错误的模式,并且每次更改变量时都必须重新编译资产,这违反了进行其他检查的目的。
一种简单得多的方法是添加一个类 .active
.inactive
在您的元素(正文)上。还是在脑海中输出内联css,以根据自定义用户身份显示自定义颜色等内容。
您要做什么?听起来好像您要检查自己是否在生产或开发中?在这种情况下,您可以执行以下操作:
< body class ='<%= development如果Rails.env =='开发'%>'>
甚至
< body<%= style ='background-color:red;'如果Rails.env =='development'%>
您永远不需要在CSS和javascript中使用ruby,如果您发现自己正在这样做,可能以错误的方式进行处理。至少是经过多次尝试才能很好地发现这一点。
p.s。数据属性是将变量等传递给javascript的非常有效的方法
How to define a SCSS variable in Config.rb for SCSS file[ COMPASS project ]
My Use-case
In Config.rb file something like
a = true
In style.scss i like to use the variable like
@if a == true{
background: #fff;
}
@else {
background: #000;
}
One of the solution http://worldhousefinder.com/?p=124141
But its not worked for me.
You can't/shouldn't do this. You will precompile your assets so there is no way of dynamically adapting to changing variables. This might work for your config.rb variable, but it is a bad pattern to use and you'd have to recompile your assets every time you change the variable, this defeats the purpose of doing if else checks in your sass.
A much easier approach is to either add a class .active
.inactive
on your elements (body). Or output inline css in your head for things like custom colors etc depending on users that are signed in.
What are you trying to do? It sounds like something you'd do to check whether you are in production or development? In which case you could do something like:
<body class='<%= "development" if Rails.env == 'development' %>'>
or even
<body <%= "style='background-color: red;'" if Rails.env == 'development' %>
You should never need to use ruby in your css and javascript, if you find yourself doing it you are probably approaching it in the wrong way. At least that is what I have found after many attempts to do this nicely.
p.s. data-attributes are a very effective way of passing variables, etc to javascript
这篇关于在config.rb中为scss文件创建scss变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!