P
P
Pavel Myshkin2014-09-24 17:32:18
Less
Pavel Myshkin, 2014-09-24 17:32:18

How to call a variable with the loop step suffix in a Less loop?

I want to call a variable in a cycle with a suffix equal to the step of the cycle (iteration)

@color-1: rgb(208, 101, 3);
@color-2: rgb(233, 147, 26);
@color-3: rgb(22, 145, 190);
@color-4: rgb(22, 107, 162);
@color-5: rgb(27, 54, 71);
@color-6: rgb(21, 40, 54);

.loop (@i) when (@i <= 6) {
        [email protected]{i} {
            color: @color-(@i); // Неверно ... не могу понять, как написать 
        }
        .loop(@i + 1);
    }
    .loop (0) {}
    .loop (1);

I want a result like this:
// Код на LESS 
.menu-item_1 {color: @color-1;}
.menu-item_2 {color: @color-2;}
.menu-item_3 {color: @color-3;}
.menu-item_4 {color: @color-4;}
.menu-item_5 {color: @color-5;}
.menu-item_6 {color: @color-6;}

There is a solution, but not what I want:
.bgcolor (@num) when (@num = 1) {background: @color-1}
.bgcolor (@num) when (@num = 2) {background: @color-2}
.bgcolor (@num) when (@num = 3) {background: @color-3}
.bgcolor (@num) when (@num = 4) {background: @color-4}
.bgcolor (@num) when (@num = 5) {background: @color-5}
.bgcolor (@num) when (@num = 6) {background: @color-6}

.loop (@i) when (@i <= 6) {
    [email protected]{i} {
        .bgcolor(@i);
    }
    .loop(@i + 1);
}
.loop (0) {}
.loop (1);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
monochromer, 2014-11-07
@monochromer

I think it's better to drive the colors into an array

@colors: rgb(208, 101, 3), rgb(22, 107, 162), rgb(21, 40, 54);
@length: length(@colors);

.loop(@i) when (@i <= @length) {
  [email protected]{i} {
    color: extract(@colors, @i); 
  }
  .loop(@i + 1);
}

.loop(1);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question