7

How do I set a SCSS variable based on ruby variable value?

Following code is working for CSS but not for SCSS:

- primaryColor = app.theme_variables['primary-color'];
= content_for(:after_head_script) do
  css:
   .primaryColor {
     color: #{primaryColor};
   }

If I do same thing with SCSS:

- primaryColor = app.theme_variables['primary-color'];
= content_for(:after_head_script) do
  scss:
    $color = #{primaryColor}

    .primaryColor {
      color: $color;
    }

output is as follows:

.primaryColor{color:primaryColor}

So any idea on how to inject variable into SCSS?

Gapipro
  • 1,913
  • 2
  • 22
  • 34

3 Answers3

1

Sadly, it's not supported.

Iterpolated are:

  • ruby
  • javascript
  • css
  • markdown
  • textile
  • rdoc

And these are not:

  • sass/scss
  • less
  • coffee

As described here: https://github.com/slim-template/slim#embedded-engines-markdown-

Astery
  • 1,216
  • 13
  • 22
-1

Add a .slim extension to your .scss file and your variable expansions should work (i.e., ".scss.slim").

See Asset Pipeline Preprocessing for details.

Greg Tarsa
  • 1,622
  • 13
  • 18
-1

You can use standard ERB interpolation inside scss directives, not sure if there is a slim-er way.

scss:
  $color = <%= primaryColor %>;
João Costa
  • 344
  • 3
  • 11