Pure LESS (assuming you're using Web Essentials 2013 which uses LESS 1.5.x):
@icons: upvote, downvote, comment, new, notify, search, popup, eye, cross;
.iconize();
.iconize(@i: length(@icons)) when (@i > 0) {
.iconize((@i - 1));
@value: extract(@icons, @i); // LESS arrays are 1-based
.@{value} {background-position: (-20px * (@i - 1)) 0}
.color.@{value} {background-position: (-20px * (@i - 1)) -20px}
.white.@{value} {background-position: (-20px * (@i - 1)) -40px}
}
I removed &
from selector names since it has no effect when you generate these classes in the global scope (but put it back if you actually need .iconize
to be nested in another ruleset). It is also possible to calculate array length in earlier LESS versions (that have no length
function) w/o any javascript, but I don't list this method here since it's quite scary (and you don't need it anyway).
Your javascript based loop is in fact less or more correct but the problem is all values returned by LESS inline javascript are of so-called "anonymous value" type and not a numbers so that when (@c < @count)
condition is always true and the loop becomes infinite. (basically the condition is expanded exactly as when (0 < ~'9')
... when (9 < ~'9')
= true etc.)