ceth
ceth

Reputation: 45325

Iterations in Stylus

I have Stylus file which looks like:

div.c0
   background-color $background-color0
   color $color-0

div.c2
   background-color $background-color2
   color $color-2

div.c4
   background-color $background-color4
   color $color-4

div.c8
   background-color $background-color8
   color $color-8

div.c16
   background-color $background-color16
   color $color-16

Can I use Stylus iterations here to simplify my Stylus file ?

Upvotes: 0

Views: 257

Answers (1)

kizu
kizu

Reputation: 43244

While there is no interpolation (yet) for variables, you can use lookup bif to get the values of such variables. So, for you case, the code would be this:

for $i in 0..4
  $num = $i ? 1 : 0
  $num = $num * 2 for $j in 0..($i - 1)
  div.c{$num}
    background-color: lookup('$background-color' + $num)
    color: lookup('$color' + $num)

Upvotes: 1

Related Questions