M
M
Michael R.2018-06-27 11:32:54
Sass
Michael R., 2018-06-27 11:32:54

How to correctly pass a variable to a mixin (SCSS)?

Greetings!
I'm trying to implement a certain mixinone that will do all the template work, and all I have to do is pass the necessary constructions into it in the form of classes and their properties.
What should be the output:

.test-1 {
  color: red;
}

.test-2 {
  color: red;
}

.test-3 {
  color: red;
}

How I see it:
@mixin get() {
  @for $i from 1 through 3 {
    @content;
  }
}

@include get() {
  .test-#{$i} {
    color: red;
  }
}

In my version, the compiler swears that the variable $idoesn't exist yet, and I'm trying to pass it to mixin. I understand the mistake, but how then to solve the problem with the assignment $iat the end of the class name testinside the body mixin get?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Matvey Phoenix, 2018-06-27
@Mike_Ro

Content blocks passed to a mixin are evaluated in the same scope where the block is defined, not the mixin. This means that the mixin's local variables cannot be used in the passed content block and the variables will be treated as global - source .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question