Creating or referencing variables dynamically in Sass

后端 未结 6 1776
臣服心动
臣服心动 2020-11-22 01:31

I\'m trying to use string interpolation on my variable to reference another variable:

// Set up variable and mixin
$         


        
6条回答
  •  难免孤独
    2020-11-22 01:58

    Here's another option if you're working with rails, and possibly under other circumstances.

    If you add .erb to the end of the file extension, Rails will process erb on the file before sending it to the SASS interpreter. This gives you a can chance to do what you want in Ruby.

    For example: (File: foo.css.scss.erb)

    // Set up variable and mixin
    $foo-baz: 20px; // variable
    
    <%
    def do_this(bar)
      "width: $foo-#{bar};"
    end
    %>
    
    #target {
      <%= do_this('baz') %>
    }
    

    Results in the following scss:

    // Set up variable and mixin
    $foo-baz: 20px; // variable
    
    #target {
      width: $foo-baz;
    }
    

    Which, of coarse, results in the following css:

    #target {
      width: 20px;
    }
    

提交回复
热议问题