Creating or referencing variables dynamically in Sass
sass, variables
Solution
Sass does not allow variables to be created or accessed dynamically. However, you can use lists for similar behavior.
scss:
$list: 20px 30px 40px;
@mixin get-from-list($index) {
width: nth($list, $index);
}
$item-number: 2;
#smth {
@include get-from-list($item-number);
}
css generated:
#smth {
width: 30px;
}
- http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#lists
- http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#list-functions
Problem
I'm trying to use string interpolation on my variable to reference another variable: ``` // Set up variable and mixin $foo-baz: 20px; @mixin do-this($bar) { width: $foo-#{$bar}; } // Use mixin by passing 'baz' string as a param for use $foo-baz variable in the mixin @include do-this('baz'); ``` But when I do this, I get the following error: Undefined variable: "$foo-". Does Sass support PHP-style variable variables?