Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass variable interpolation with backslash in output

I'm creating some icon font rules for using in my site. Using Sass I wanted to list all the icons in a list variable and use @each to loop through them all.

Code looks like this:

$icons: 
    wifi 600,
    wifi-hotspot 601,
    weather 602;

@each $icon in $icons {
    .icon-#{nth($icon, 1)}, 
    %icon-#{nth($icon, 1)} {
        content: "\#{nth($icon, 2)}";
    }
}

The problem is the backslash on the content: line. I need it for the character encoding, but it escapes the variable interpolation, outputting CSS that looks like this:

.icon-wifi {
  content: "\#{nth($icon, 2)}";
}

Adding one more backslash like this: content: "\\#{nth($icon, 2)}"; outputs this CSS:

.icon-wifi {
  content: "\\600";
}

Is there a way to get the Sass to output CSS with only a single backslash while keeping the variable interpolation?

like image 235
Ben Avatar asked Feb 06 '14 16:02

Ben


People also ask

How do you use interpolation in sass?

Interpolation is a new principle and it is widely used in SASS. To interpolate an expression we need to wrap the expression using #{ }. where ….. represents some text. Interpolation in SASS expressions always returns an unquoted string, no matter whether the string is quoted or not.

What is #{} in SCSS?

is the css class selector, #{} interpolation syntax it allows the using of the variables in selectors and property names $name: foo; $attr: border; p. #{$name} { #{$attr}-color: blue; }


2 Answers

I got this to work by messing with the interpolation

sassmesiter demo

// ----
// Sass (v3.4.21)
// Compass (v1.0.3)
// ----

$icons: 
    wifi 600,
    wifi-hotspot 601,
    weather 602;

@each $icon in $icons {
    .icon-#{nth($icon, 1)}, 
    %icon-#{nth($icon, 1)} {
        content: #{'"\\' + nth($icon, 2) + '"'}; // <------ See this line
    }
}

compiles to

.icon-wifi {
  content: "\600";
}

.icon-wifi-hotspot {
  content: "\601";
}

.icon-weather {
  content: "\602";
}
like image 112
aaaaaa Avatar answered Oct 29 '22 18:10

aaaaaa


Use unquote and double slash

$var:123 → content:"\e123"

 content:#{unquote('\"')+("\\")+("e")+$var+unquote('\"')};
like image 39
Kareem Avatar answered Oct 29 '22 20:10

Kareem