Less dynamic class-names generated at runtime, or classes generated from array loop

Tuesday, July 8, 2014

Currently, one of my developers have developed this less file:



.countryFlag( @countryName :"DK"){
content: url("/images/flags/@{countryName}.gif") no-repeat center;
}

a.flag-DK {
.countryFlag ();
}

a.flag-DE {
.countryFlag (DE);
}

a.flag-EE {
.countryFlag (EE);
}

...


with some 20 countries. I was wondering if this can be done smarter with less, either using loops or dynamic names created at runtime. Consider the following pseudo-code that describes what I want:



a.flag-@{country} {
content: url("/images/flags/@{country}.gif") no-repeat center;
}


that simply matches any definitions. I am aware that this could create lots of conflicts, but perhaps it could be further narrowed down via regex? Makes sense, but haven't been able to find this.


An alternative could be to create the "static" css classes using a loop of some sort. Consider this pseudo-alternative:



@countries: 'dk', 'de', 'uk', 'us', ...;

for(country : countries) {
a.flag-@{country} {
content: url("/images/flags/@{country}.gif") no-repeat center;
}
}


and thus create the classes for a pre-determined list of countries.


Is either of these alternatives available in some sort of way? Or can you perhaps advice on a third option that I might have overlooked? I feel kinda stupid when I see 20-something almost identical classes defined, having only a small string in difference, and think that a CSS pre-processor MUST be able to do this smarter.


Thanks!







http://ift.tt/1qQJqC1