3

I'm looking to get a solid answer on whether it's possible to reference variables based on the value of another variable. This is possible in PHP (among others).

Example (in Sass):

I want to set the border-color to $green (or $blue, $yellow, etc), which are variables being included from a partial. I am looping through a list of color names like such:

$colors = green, yellow, blue; 

@each $c in $colors {
&.#{$c} {
    border-color: $#{$c};
    }
}

The above code (ideally) generates classes for .green, .yellow, and .blue, each with a border-color set to $green, $yellow or $blue, respectively.

(Obviously) the above code is not working, but is there another way of achieving this in Sass/Compass?

Mike
  • 3,331
  • 2
  • 18
  • 22

2 Answers2

2

Update: I checked with @nex3, lead developer of Sass

In the google groups (mentioned below) discussion he actually said this kind of variable-interpolation is taking abstraction the wrong way and not what Sass intends to do. When I asked him, if he still holds true to that discussion, which was posted quite sometime back, here's what he had to say:

yes, especially since we're adding maps in Sass 3.3, which will support most use cases of variable interpolation anyway

Seems like Sass 3.3 will have a support for this via a feature called maps Unable to find any documentation on that for now. If anyone can find links to it, please share


This kind of meta programming is not currently supported in Sass https://groups.google.com/forum/?fromgroups#!topic/sass-lang/upr78cyrW1I

Vinay Raghu
  • 1,269
  • 2
  • 13
  • 22
2

Looks like a duplicate of this thread: using hash with scss (which I've been watching/secretly pecking at for a few months now)

As Leo points out, you can try to work around this -- but PHP-style variable variables aren't something that the language currently supports.

Community
  • 1
  • 1
founddrama
  • 2,391
  • 1
  • 22
  • 31