possible de définir un tableau dans SASS?
Question
Je me demande s'il est possible d'utiliser un tableau avec Sass car je trouve mon auto-répétant le genre de chose suivant:
.donkey
h2
background-color= !donkey
.giraffe
h2
background-color= !giraffe
.iguana
h2
background-color= !iguana
La solution
Non, ce n'est pas possible. La meilleure façon de le faire est de définir un mixin:
+animal-h2(!name, !color)
.#{name} h2
background-color= !color
Ensuite, vous pouvez avoir une ligne par style, plutôt que trois:
+animal-h2("donkey", !donkey)
+animal-h2("giraffe", !giraffe)
+animal-h2("iguana", !iguana)
Autres conseils
Absolument.
$animals: "donkey", "giraffe", "iguana"
@foreach $animal in $animals
.#{$animal}
h2
background-color: !#{$animal}
La réponse de Nex3 est correcte. Pour que cela fonctionne avec SASS sur Rails 3.1, je devais avoir ce qui suit dans un fichier CSS.SCSS:
$donkey: #CC4499;
@mixin animal-h2($name, $color) {
.#{$name} h2 {
background-color: $color;
}
}
@include animal-h2("donkey", $donkey);
@include animal-h2("horse", #000);
Quelle sortie:
.donkey h2 {
background-color: #CC4499;
}
.horse h2 {
background-color: black;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow