I’m trying to declare background image icons for some table rows:
.i(@file:'file.png', @type) {
&.@{type} {
td:first-child {
background-image: url('../img/@{file}');
}
}
}
I’d like to be able to pass in multiple image types at once:
.i('code.png', 'asp, php, rb, py')
and have it effectively do this:
.i(@file:'file.png', @type) {
&.@{type1},
&.@{type2},
&.@{type3},
&.@{type4}, {
td:first-child {
background-image: url('../img/@{file}');
}
}
}
I know the CSS output will be different, the last code example is for illustration purposes.
Any ideas on how to achieve this, short of just declaring a bunch of empty selectors as placeholders?
Updated for LESS 1.5
This code produces the same effect more efficiently in the later versions of LESS, using the newer
extract()andlength()functions available in LESS 1.5+. Output will be the same as the original example.With Loops and Inline-Javascript in LESS 1.3.3
This took a few hours to come up with (no, I didn’t have a bunch of free time to work on it, I’m just hopelessly addicted…). One of the parts that took the longest was figuring out why my
@stopIndexwas not being seen as a number by LESS when I was returning the.lengthof the array, and throwing a type error. I finally discovered I need to explicitly tell it to see it as a number using theunit()function of LESS.The solution utilizes general concepts from these sources:
LESS
CSS Output