Description
Looking at the new docs I see that list variables are supported.
@list: apple, satsuma, banana, pear;
I expect one of the most common use cases for lists is outputting a series of rules, such as these.
.fruit-apple {
content: "Just a test.";
}
.fruit-satsuma {
content: "Just a test.";
}
.fruit-banana {
content: "Just a test.";
}
.fruit-pear {
content: "Just a test.";
}
The current LESS syntax to do this however, is completely insane. ((Though I do understand it was an unintentional "feature"))
@list: apple, satsuma, banana, pear;
.loop(@index) when (@index <= length(@list)) {
@item: extract(@list, @index);
.fruit-@{item} {
content: "Just a test.";
}
.loop(@index + 1);
}
.loop(1);
I think we should add some nice clean declarative syntaxes to handle the common cases where you'd think you want a for loop. ie: Something declarative like the @when
idea was, rather than the if/else ideas which weren't.
I haven't thought of the best syntax for this yet, and I'd like other ideas too.
The first and simplest possible syntax would be to auto-expand lists inside of selectors:
@list: apple, satsuma, banana, pear;
.fruit-@{list} {
content: "Just a test.";
}
However this isn't that great because there's no obvious way to use the current value inside of the selector (ie: It would be an ugly hack to support content: "Just a test @{item}";
or content: "Just a test @{fruit}";
) and it's rather non-obvious that this selector expands to four different rules instead of one.
Another idea might be a syntax like this:
@list: apple, satsuma, banana, pear;
@expand @fruit in @list {
.fruit-@{fruit} {
content: "Just a test @{fruit}";
}
}
This one is rather straightforward, it properly supports an item variable, while also becoming flexible enough to do more than just .fruit-@{fruit}
. Though it needs a bit of bike-shedding over whether to call it @expand
or something like @for
, @foreach
, or @each
, whether it should be @fruit in @list
or @list as @fruit
.
Or maybe given the current style of when() maybe a syntax like this:
@list: apple, satsuma, banana, pear;
& each (@list as @fruit) {
.fruit-@{fruit} {
content: "Just a test @{fruit}";
}
}
You could also make it a little more like when (...)
by putting it directly on the .fruit-*
selector though that might read a little backwards (since the variable in the selector is declared in the each that comes 'after'.
@list: apple, satsuma, banana, pear;
.fruit-@{fruit} each (@list as @fruit) {
content: "Just a test @{fruit}";
}