Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a variable is defined in SASS

Tags:

sass

ruby

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)

I've found the Ruby equivalent which is:

defined? foo

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

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

like image 917
locrizak Avatar asked Dec 14 '11 22:12

locrizak


People also ask

How do you check if a variable is defined?

Use the typeof operator to check if a variable is defined or initialized, e.g. if (typeof a !== 'undefined') {} . If the the typeof operator doesn't return a string of "undefined" , then the variable is defined.

How is variable defined in Sass?

Sass variables are simple: you assign a value to a name that begins with $ , and then you can refer to that name instead of the value itself. But despite their simplicity, they're one of the most useful tools Sass brings to the table.

Can we use if condition in SCSS?

If the condition proves true, the compiler will execute the if statement's code block. If the condition proves false, the compiler will move on to code outside and below the if statement's code block. An if statement is written with the @if rule, followed by the condition we want to evaluate and a code block.

How do you change a variable in Sass?

A Sass variable must be initialized with a value. To change the value, we simply replace the old value with a new one. A variable that's initialized inside a selector can only be used within that selector's scope. A variable that's initialized outside a selector can be used anywhere in the document.


2 Answers

For Sass 3.3 and later

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

  • 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. ...

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)

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.

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

@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 false is a valid value for your variable, you can use:

@mixin my_mixin() {   $base-color: null !default;    @if $base-color != null {      color: $base-color;   } } 
like image 195
RobW Avatar answered Sep 29 '22 01:09

RobW


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

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.

Example:

In specific-variables.scss you have:

$brand: "My Awesome Brand"; 

In default-variables.scss you have:

$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";

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

like image 38
Blackbam Avatar answered Sep 29 '22 00:09

Blackbam