ruby 检查变量是否在 SASS 中定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8512802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:35:17  来源:igfitidea点击:

Checking if a variable is defined in SASS

rubysass

提问by locrizak

AS the title says I am trying to check whether a variable is defined in SASS. (I am using compass if that makes any different difference)

正如标题所说,我正在尝试检查 SASS 中是否定义了变量。(如果有任何不同,我正在使用指南针)

I've found the Ruby equivalent which is:

我找到了 Ruby 等价物,它是:

defined? foo

Gave that a shot in the dark but it just gave me the error:

在黑暗中试了一下,但它给了我错误:

defined": expected "{", was "?

defined": expected "{", was "?

I've found a work around (which is obviously just to define the variable in all cases, which in this case it actually makes more sense) but I'd really like to know if this is possible for the future

我找到了一个解决方法(这显然只是为了在所有情况下定义变量,在这种情况下它实际上更有意义)但我真的很想知道这是否可能在未来

回答by RobW

For Sass 3.3 and later

对于 Sass 3.3 及更高版本

As of Sass 3.3 there is a variable-exists()function. From the changelog:

从 Sass 3.3 开始,有一个variable-exists()函数。从变更日志

  • It is now possible to determine the existence of different Sass constructs using these new functions:
    • variable-exists($name)checks if a variable resolves in the current scope.
    • global-variable-exists($name)checks if a global variable of the given name exists. ...
  • 现在可以使用这些新函数来确定不同 Sass 结构的存在:
    • variable-exists($name)检查变量是否在当前范围内解析。
    • global-variable-exists($name)检查给定名称的全局变量是否存在。...

Example usage:

用法示例:

$some_variable: 5;
@if variable-exists(some_variable) {
    /* I get output to the CSS file */
}
@if variable-exists(nonexistent_variable) {
    /* But I don't */
}


For Sass 3.2.x and earlier (my original answer)

对于 Sass 3.2.x 及更早版本(我的原始答案)

I ran into the same problem today: trying to check if a variable is set, and if so adding a style, using a mixin, etc.

我今天遇到了同样的问题:尝试检查是否设置了变量,如果设置了,则添加样式、使用 mixin 等。

After reading that an isset()function isn't going to be added to sass, I found a simple workaround using the !defaultkeyword:

在阅读了不会将isset()函数添加到 sass 之后,我找到了一个使用!default关键字的简单解决方法:

@mixin my_mixin() {
  // Set the variable to false only if it's not already set.
  $base-color: false !default;

  // Check the guaranteed-existing variable. If it didn't exist 
  // before calling this mixin/function/whatever, this will 
  // return false.
  @if $base-color {
     color: $base-color;
  }
}

If falseis a valid value for your variable, you can use:

如果false是变量的有效值,则可以使用:

@mixin my_mixin() {
  $base-color: null !default;

  @if $base-color != null {
     color: $base-color;
  }
}

回答by Blackbam

Just as a complementary answer - you should have a look on the defaultkeyword for certain use cases. It gives you the possibility to assign a default value to variables in case they are not defined yet.

作为补充答案 - 您应该查看default某些用例的关键字。它使您可以为尚未定义的变量分配默认值。

You can assign to variables if they aren't already assigned by adding the !default flag to the end of the value. This means that if the variable has already been assigned to, it won't be re-assigned, but if it doesn't have a value yet, it will be given one.

如果变量尚未分配,则可以通过将 !default 标志添加到值的末尾来分配给变量。这意味着如果变量已经被赋值,它不会被重新赋值,但如果它还没有值,它就会被赋值。

Example:

例子:

In specific-variables.scss you have:

在 specific-variables.scss 你有:

$brand: "My Awesome Brand";

In default-variables.scss you have:

在 default-variables.scss 你有:

$brand: company-name !default;
$brand-color: #0074BE !default;

Your project is built like this:

你的项目是这样构建的:

@import "specific-variables.scss"; @import "default-variables.scss"; @import "style.scss";

@import "特定变量.scss"; @import "default-variables.scss"; @import "style.scss";

The value of brand will be My Awesome Brandand the value of brand color will be #0074BE.

品牌的价值将是我的真棒品牌,品牌颜色的价值将是 #0074BE

回答by Dirigible

If you're looking for the inverse, it's

如果你正在寻找相反的东西,那就是

@if not variable-exists(varname)

To check if it is undefined or falsy:

要检查它是否未定义或虚假:

@if not variable-exists(varname) or not $varname

And if you want to set it only if it's undefined or null

如果您只想在未定义或为空时才设置它

$varname: VALUE !default;