@color-purple: "#ffffff"
@colors: purple, light-purple, green, light-green, red, light-red, grey, light-grey, lightest-grey;
.ColorsMixin(@i:0) when(@i =< length(@colors)){ //loop over icons array
@color: extract(@colors, @i); //extract the icon at current index @i
.color--@{color}{
background: @{color-@{color}};
&:before{
content: "@{color}";
}
&:after{
content: "\@{color-@{color}}";
}
}
.ColorsMixin(@i + 1);
}
.ColorsMixin();
So, I can get it to do what I want to do in the
content: "\@{color-@{color}}";
part. This will output
content: "#ffffff";
However, when I try to output the @color-purple variable as the background, LESS throws an error. It only seems to work if I wrap it in quotation marks, but the background property wants the hex code without the quotes around it.
What's the trick here?
is not valid Less syntax, the proper one would be:
Note however, the very idea of indirectly refering to a variable values via escaping is a durty kludge (quite wide-spread but still very dirty). It works when you assign such value directly to CSS property, but it will fail for anything else, simply because such value is not a color anymore but an unquoted string with an unknown content... E.g. the following code will fail:
The proper Less method of getting a variable value via its name is "variable reference", e.g.:
Additionally, take a time to consider if the whole approach of having all these colors as distinct variables and then having a separate list of these variables names is really what you need. Normally a single list having both color names and values is not such awfully bloating and much more maintainable.