0

In SASS I would like to generate some css only if a map exists. For this I have to check first, if the map exists or not. It should work similar to this:

$mymap: (
   width:   10px,
   color:   blue,
);


@if MAP-EXISTS($mymap) {
   .myclass { 
      width: map-get($mymap, width);
      color: map-get($mymap, blue);
   }
}

Due to the fact that a function 'MAP-EXISTS()' does not exist in SASS: I am looking for a way to do this on another way. But I cannot figure it out.

So my Question is: How can I check if a map exists in SASS?

(SASS Version is V3.4.9)

Thx for answers and help.

  • No: it is not a duplicate. I have seen this post. But the way checking a variable unfortunately does not works on maps. –  Dec 17 '14 at 11:20
  • You're either doing something wrong or there isn't enough code here to reproduce the problem. If the variable doesn't exist at all, this is a duplicate of the other question. If the variable does exist but the index doesn't, then the answer you've already received is the answer (but you claim that it doesn't). That leads me to believe you're doing something wrong. – cimmanon Dec 17 '14 at 11:51
  • Thx 4u reply and help. Of course it could be I am wrong, sometimes there are to much knots in the head :-( What do you mean with 'If the variable does not exist ...'? The task is not to check a variable at all but to check a map ... as I tried twice (honestly: before starting this thread) wether the sass function 'variable_exists()' nor working with a default value for the map worked a described in the other thread ... Note: The code is complete. The class should only been written if the map exists ... but cause the map is part of a seperated configuration it is not sure if the the map is set. –  Dec 17 '14 at 12:17
  • Possibly related: http://stackoverflow.com/questions/23277103/sass-check-which-kind-of-value-a-variable-have or http://stackoverflow.com/questions/25396956/global-variable-exists-is-triggering-errors-in-sass – cimmanon Dec 17 '14 at 12:52

1 Answers1

0

There isn't a way to check if a map exist, but you can check if map-has-key which returns true or false whether map has a value associated with a given key which may help you as an alternative solution.

An example:

$mymap: (
   width:   10px,
   color:   blue,
);

.map-exist { 

  $map-existance: map-has-key($mymap, width);
  $map-existance: map-has-key($mymap, color);

  // Uncomment the line bellow to see that if a value doesn't exist
  // it won't apply any propertie

  // $map-existance: map-has-key($mymap, padding);

  @if $map-existance == true {
    width: map-get($mymap, width);
    color: map-get($mymap, color);
  }
}

A demo: http://sassmeister.com/gist/f7ecb5b1fb2781cc3225

Vangel Tzo
  • 8,885
  • 3
  • 26
  • 32
  • Thx 4u reply. Indeed I tested some similar ways7methods before. The Problem is: even it $mymap DOES NOT EXIST it throws me out an error and stops compiling at all. But I need a solution with a method which only don't do anything if the map does not exist and goes further on ... When I searched for a solution: In the sass documentaton I only found map functions which throws errors if the map doesn't exist. Maybe there is a sass way to handle with the error and to go on ... or another way to check if a map exist ... - I think, there should be one :-) –  Dec 17 '14 at 11:07