SASS: Set variable at compile time

孤街浪徒 提交于 2019-11-28 03:12:09

问题


Is it possible to set a sass variable at compile time? I basically want to do this:


$color: red !default;
div#head {

  background-color: $color;

}

When I compile to css I want to set $color to "blue" (preferably from the command line). Has anyone been able to do this?

Thanks, Chris


回答1:


I found this at their FAQ http://sass-lang.com/docs/yardoc/file.FAQ.html

If you just want to pass some variables to the CSS every time it gets compiled, like using --watch, you can use Sass functions to define Ruby scripts to even query a database. But the code is going to be compiled only once, and served statically.

But if you need to recompile it at every request with different options,

you can use Sass::Engine to render the code, using the :custom option to pass in data that can be accessed from your Sass functions

Seems like it's not recommended, though. Probably for performance reasons.




回答2:


An alternate of command line options is to create other files assigning values to variables.

Assume that your code above is in a file named 'style.scss'. To set $color to "blue", create a file such as:

$color: blue;
@import "style";

and name it to 'blue.scss' for example. Then compile it with below.

sass blue.scss:style.css

When you want to assign another value to the variable, make another file named "green.scss" like:

$color: green;
@import "style";

Then compile it with

sass green.scss:anotherstyle.css

It is bothering somewhat but enables to decide values of variables at compile time.



来源:https://stackoverflow.com/questions/5589067/sass-set-variable-at-compile-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!