7

On the site I'm working on we were using Scaffold, which is a PHP-based system similar to Sass. It also can process Sass functions\files. Unfortunately that system is now abandonware, and we are looking on a way to move completely to Sass. There is one big feature with Scaffold though that I'm not finding a way to port to Sass, the variable groups.

Variable in Scaffold can be organized in groups and used with a point-separated markup. For example I would define them as:

@variables vargroup1{
    variable1: ####;
    variable2: ####;
    variable3: ####;
    variable4: ####;
}

And later use on the code as, for example.

body{ width: vargroup1.variable1; margin: vargroup1.variable2 + 10;}

This helps development a lot, since you can group together variables from a system and reading the CSS files you can easily know what to reference. I didn't find anything like that on the Sass documentation, anyone knows if it is possible? Or if there is anyway using Mixins to do this?

Thanks

Volker E.
  • 5,911
  • 11
  • 47
  • 64
Yohan Leafheart
  • 860
  • 1
  • 11
  • 27
  • Does this answer your question? [SASS - get map item value by item index](https://stackoverflow.com/questions/40468351/sass-get-map-item-value-by-item-index) – Sean Feb 07 '23 at 12:05

4 Answers4

5

I came across this somewhat clunky solution (see Chris Eppstein's reply) using zip and index. Apparently a maintainer of SASS added these built-in functions in response to a similar question.

To quote his reply:

$border-names: a, b, c;
$border-widths: 1px, 1px, 2px;
$border-styles: solid, dashed, solid;
$border-colors: red, green, blue;
$borders: zip($border-widths, $border-styles, $border-colors);

@function border-for($name) {
   @return nth($borders, index($border-names, $name))
}

@each $name in $border-names {
  .border-#{$name} {
    border: border-for($name);
  }
}

Would generate:

.border-a { border: 1px solid red; }
.border-b { border: 1px dashed green; }
.border-c { border: 2px solid blue; }

The "naming your variables" comes from the list "-names" at the top; you then use the index of a desired variable name from that variable list to get the nth value from another variable lists. zip is used to mush separate lists together, so that you can retrieve the same index from all lists at the same time. Wrapping that behavior in a function makes it easier to retrieve a set.

drzaus
  • 24,171
  • 16
  • 142
  • 201
  • hmm...multidimensional arrays seem nicer -- see [SASS @each with multiple variables](http://stackoverflow.com/questions/6572588/sass-each-with-multiple-variables) and the ["more elegant" solution](http://stackoverflow.com/a/8380156/1037948) – drzaus Feb 13 '13 at 19:20
  • wait - zip is making the multidimensional array. nevermind; I'll leave the link above because it's still useful. – drzaus Feb 13 '13 at 19:22
4

There is no equivalent in Sass. But I can think in two workarounds:

1) Sass lists and its related list functions.

Your code could look like the following:

$variables = 40px 30px 20px 10px;

body {width: nth($variables, 1); margin: nth($variables, 2) + 10;}

It's not the same because list indexes can't be strings, so you haven't any way to name your variables.

2) Define a custom function. Look at Function Directives section in Sass reference

@function variables($variable_name) {
  @if ($variable_name == 'variable1') {
    @return 40px;
  } @else if ($variable_name == 'variable2') {
    @return 30px;
  }
}

body {width: variables('variable_1'); margin: variables('variable_2') + 10;}

This way is less intuitive and uglier but you can 'name your variables'.

Volker E.
  • 5,911
  • 11
  • 47
  • 64
Waiting for Dev...
  • 12,629
  • 5
  • 47
  • 57
1

You could use the scss/sass map function:

@use "sass:map";

$variables: (
    "variable1": ####;
    "variable2": ####;
    "variable3": ####;
    "variable4": ####;
}

body {
    width: map.get($variables, "variable1"); 
    margin: map.get($variables, "variable2") + 10;
}

Documentation

Tombalabomba
  • 181
  • 1
  • 7
0

You can use SASS lists a it's related functions on a way similar to that:

// List order: top, bottom, left, right, width, height, ...
$Header: 10px,auto,10px,auto,100%,50px;
$Footer: auto,0px,0px,auto,100%,20px;

@function getVar($variable,$name:top){
    $var_index:1;
    @if $name==bottom {
        $var_index:2;
    } @else if $name==left {
        $var_index:3;
    }
    // Continue de if else for each property you want.
    return nth($variable,$var_index);
}

That way calling something like:

getVar($Header,left)

Should return the value of the left property for the list of Header, but changing it to getVar($Footer,top) would return the value for the top property of the "Footer Group" (Footer List of Values).

That works for the time of using the values, but a the definition, you must follow the exact order and cannot leave any empty value, the nearest to an empty value that I found is #{''} what means "Empty String with no quotes", an empty value, but is added to the CSS.

Jay D
  • 3,263
  • 4
  • 32
  • 48
Asfhy
  • 1