测试是否定义了Sass函数

时间:2013-12-20 15:29:39

标签: sass

如何测试是否存在Sass函数?


我有一个使用自定义Sass函数的Sass包。但是,在Ruby之外(例如libsass),可能无法定义该函数并提供回退:

url: if($bootstrap-sass-asset-helper, twbs-font-path('name.eot'), 'name.eot')

我想将$bootstrap-sass-asset-helper的值设置为true或false,具体取决于是否声明了twbs-font-path

2 个答案:

答案 0 :(得分:10)

当前版本的Sass具有function-exists功能。 Full example

.foo {
  @if function-exists(myfunc) {
    exists: true;
  }
  @else {
    exists: false;
  }
}

这是v3.3的新功能(2014年3月),因此您可能需要更新Sass gem才能使用它。

Sass v3.3也增加了其他存在测试:

variable-exists($name)
global-variable-exists($name)
mixin-exists($name)

More on Sass v3.3.

答案 1 :(得分:6)

Sass没有这样的功能。如果你需要,你可以像这样捏造它:

@debug if(foo() == unquote("foo()"), false, true); // false

@function foo() {
    @return true;
}

@debug if(foo() == unquote("foo()"), false, true); // true

这是有效的,因为当你调用一个不存在的函数时,Sass假定你可能正在编写有效的CSS(calc,linear-gradient,attr等)。当发生这种情况时,你得到的是一个字符串。因此,如果函数存在,则获得函数的结果而不是字符串。所以foo() == unquote("foo()")只是检查你是否有字符串。

相关:Can you test if a mixin exists?

相关问题