I need to convert numbers into words, so:
The numbers are generated in a loop, which should output a bunch of different class names like one-third
or one-half
:
$number = 3;
@for $i from 1 through $number-1 {
// some calculations to output those classes: ".one-third", ".two-thirds"
// The following currently outputs class names like ".1-3" and ".2-3"
.#{$i}-#{$number} {
// CSS styles
}
}
I think I need to use two different associative arrays, which in PHP (just as an example) might look like:
$1 = array(
"1"=>"one",
"2"=>"two",
"3"=>"three"
);
$2 = array(
"1"=>"whole",
"2"=>"half",
"3"=>"third"
);
Is it possible in SASS/SCSS to create associative arrays at all or is there any workaround?
In Sass < 3.3 you can use multidimensional lists:
$numbers: (3 "three") (4 "four");
@each $i in $numbers {
.#{nth($i,2)}-#{nth($i,1)} {
/* CSS styles */
}
}
DEMO
In Sass >= 3.3 we get maps:
$numbers: ("3": "three", "4": "four");
@each $number, $i in $numbers {
.#{$i}-#{$number} {
/* CSS styles */
}
}
DEMO
So in terms of fractions, you could just do something in this direction, so that you don't need multiple lists or maps:
$number: 6;
$name: (
("one"),
("two" "halv" "halves"),
("three" "third" "thirds"),
("four" "quarter" "quarters"),
("five" "fifth" "fifths"),
("six" "sixth" "sixsths")
);
and then whatever you want to do with your loops ... maybe even something like this =D
@for $i from 1 to $number {
@for $j from 2 through $number {
.#{ nth( nth( $name, $i ), 1 ) }-#{
if( $i>1,
nth( nth( $name, $j ), 3 ),
nth( nth( $name, $j ), 2 )
)} {
/* CSS styles */
}
}
}
DEMO
(I wrote it this way so that you can notice in @for
, that using to
goes to n - 1
)
in addition to answer by Martin, my example for using colors as variables, that also works with color-processing functions like darken()
:
$blue: rgb(50, 57, 178);
$green: rgb(209, 229, 100);
$orange: rgb(255, 189, 29);
$purple: rgb(144, 19, 254);
$colors: (
"blue": $blue,
"green": $green,
"orange": $orange,
"purple": $purple
);
@each $name, $color in $colors {
.tc-#{$name} { color: #{$color} !important; }
.bgc-#{$name} { background-color: #{$color} !important; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With