Итак, благодаря вашей помощи, а также циклу for-in, о котором я узнал из следующего сообщения: Как сгенерировать CSS с циклом в less
Теперь у меня есть:
.for(@list, @code) {
& {
.loop(@i:1) when (@i =< length(@list)) {
@value: extract(@list, @i);
@code();
.loop(@i + 1);
}
.loop();
}
}
.role-varients(@orgType, @variants, @props, @type:child){
.for(@variants, {
& when (@type = append) {
&.@{orgType}.security_class_@{value} {
@props();
}
}
& when not (@type = append) {
.@{orgType}.security_class_@{value} {
@props();
}
}
});
}
@admin-roles: admin, admin_manager, admin_user, admin_manager_user;
@manager-roles: manager, admin_manager, manager_user, admin_manager_user;
@user-roles: user, admin_user, manager_user, admin_manager_user;
&.service_provider{
&.admin{
.role-varients(service_provider, @admin-roles, {display: block;});
th{
.role-varients(service_provider, @admin-roles, {display: table-cell;}, append);
}
button{
.role-varients(service_provider, @admin-roles, {display: inline-block;}, append);
}
}
&.manager{
.role-varients(service_provider, @manager-roles, {display: block;});
th{
.role-varients(service_provider, @manager-roles, {display: table-cell;}, append);
}
button{
.role-varients(service_provider, @manager-roles, {display: inline-block;}, append);
}
}
&.user{
.role-varients(service_provider, @user-roles, {display: block;});
th{
.role-varients(service_provider, @user-roles, {display: table-cell;}, append);
}
button{
.role-varients(service_provider, @user-roles, {display: inline-block;}, append);
}
}
}
&.client{
&.admin{
.role-varients(client, @admin-roles, {display: block;});
th{
.role-varients(client, @admin-roles, {display: table-cell;}, append);
}
button{
.role-varients(client, @admin-roles, {display: inline-block;}, append);
}
}
&.manager{
.role-varients(client, @manager-roles, {display: block;});
th{
.role-varients(client, @manager-roles, {display: table-cell;}, append);
}
button{
.role-varients(client, @manager-roles, {display: inline-block;}, append);
}
}
&.user{
.role-varients(client, @user-roles, {display: block;});
th{
.role-varients(client, @user-roles, {display: table-cell;}, append);
}
button{
.role-varients(client, @user-roles, {display: inline-block;}, append);
}
}
}
&.vendor{
&.admin{
.role-varients(vendor, @admin-roles, {display: block;});
th{
.role-varients(vendor, @admin-roles, {display: table-cell;}, append);
}
button{
.role-varients(vendor, @admin-roles, {display: inline-block;}, append);
}
}
&.manager{
.role-varients(vendor, @manager-roles, {display: block;});
th{
.role-varients(vendor, @manager-roles, {display: table-cell;}, append);
}
button{
.role-varients(vendor, @manager-roles, {display: inline-block;}, append);
}
}
&.user{
.role-varients(vendor, @user-roles, {display: block;});
th{
.role-varients(vendor, @user-roles, {display: table-cell;}, append);
}
button{
.role-varients(vendor, @user-roles, {display: inline-block;}, append);
}
}
}
Я бы использовал другой цикл for для обработки 3 основных блоков [service_provider, client, vendor], но там, где я использую .@{value}, я предполагаю, что область действия нарушена из-за наличия цикла внутри цикла, и он не дает ожидаемых результатов. .
Хотя приведенное выше все еще кажется длинным, это определенно лучше, чем писать каждое правило вручную.
Мое решение появилось благодаря вкладу Гарри, поэтому, наверное, мне следует отметить его ответ как правильный? Всем еще раз спасибо ;)
person
user2115620
schedule
13.06.2016