问题
I'm using a ternary-like statement to initialize a variable in Sass. This allows me to set some of my default variables to the same thing that Zurb Foundation is using, but if I decided to not include a Foundation module, then things should not fall on their head.
$nav-link-icon-color: if( global-variable-exists($topbar-link-color), $topbar-link-color, #fff) !default;
This was working fine until after I upgraded to Sass 3.4. Immediately after that, I started getting this error:
error sass/style.scss (Line 20 of sass/partials/_navigation-icons.scss: $name: #ffffff is not a string for `global-variable-exists')
$topbar-link-color
has been initialized when I check it. It's a string with the value #fff, declared like this by Foundation:
$topbar-link-color: #fff !default;
I even tried passing an uninitialized variable into global-variable-exists()
:
$nav-link-icon-color: if( global-variable-exists($happy-scrappy), $happy-scrappy, #fff) !default;
But Sass still hates that too:
error sass/style.scss (Line 21 of sass/partials/_navigation-icons.scss: Undefined variable: "$happy-scrappy".)
This is very odd to me, since the whole point of global-variable-exists()
is, ya know, checking if the variable exists. It seems like the parameter sent to global-variable-exists()
is not being parsed correctly, but I don't know.
I've tried uninstalling all versions of Sass and Compass, then reinstalling, and finally rebooting to no avail. I've even reverted back to Sass 3.3.14, which I was using before, and the same errors persist.
compass 1.0.1 Sass 3.4.0 Foundation 5.3.3 ruby 2.0.0p481 Win 7 64 bit
Edit: After further research my wrong way of doing it was working in sass 3.2.19 and compass 0.12.2.
回答1:
The "problem" persists because you're using the function incorrectly. The docs paint a very clear picture as to how this function is intended to be used:
$a-false-value: false;
// global-variable-exists(a-false-value) => true
.foo {
$some-var: false;
@if global-variable-exists(some-var) { /* false, doesn't run */ }
}
See how there are no variables being passed to the function? That's because it expects a string that contains the name of the variable, not the variable itself. Passing in the variable itself would defeat the purpose of the function: you can't pass variables that don't exist to functions or mixins.
So... just drop the $
:
$nav-link-icon-color: if( global-variable-exists(topbar-link-color), $topbar-link-color, #fff) !default;
来源:https://stackoverflow.com/questions/25396956/global-variable-exists-is-triggering-errors-in-sass