title: Dynamically render a sass file through sprockets
body: <p>I want, from a helper, to render some variables in a <code>.scss.erb</code> template that makes use of the <code>image-url()</code> sass function:</p>
  
  <pre><code>// template.scss.erb
  
  #&lt;%= id %&gt; {
    background-image: image-url('&lt;%= image_file %&gt;');
  }
  </code></pre>
  
  <p>So far, the ERB part has been easy:<br>
  <em>(leveraging <a href="https://stackoverflow.com/a/9734736/625710">this stack overflow answer</a>)</em></p>
  
  <pre class="lang-rb prettyprint-override"><code>vars_binding = OpenStruct.new(
                   id:         'foo', 
                   image_file: 'foo.jpg'
                 ).instance_eval { binding }
  
  template = File.read('path/to/template.scss.erb')
  
  rendered_sass = ERB.new(template).result(vars_binding)
  </code></pre>
  
  <p>Running that code, <code>sass</code> is now equal to:</p>
  
  <pre><code>#foo {
    background-image: image-url('foo.jpg');
  }
  </code></pre>
  
  <p>However, when I next try to run:</p>
  
  <pre class="lang-rb prettyprint-override"><code>css = Sass::Engine.new(
    rendered_sass,
    syntax:     :scss,
    cache:      false,
    load_paths: view_context.assets.paths,
    read_cache: false,
    style:      :compressed
  ).render
  </code></pre>
  
  <p>It returns </p>
  
  <pre><code>NoMethodError: undefined method `[]' for nil:NilClass
  from …/sprockets-3.2.0/lib/sprockets/sass_processor.rb:267:in `sprockets_context'
  </code></pre>
  
  <p>because the call to <code>Sass::Engine</code> doesn’t provide a Sprockets context. </p>
  
  <p>If I remove <code>image-url()</code> from the <code>.scss.erb</code> template, and replace it with the native <code>url()</code>, it will then render correctly as CSS, no problem.</p>
  
  <p><strong>So how do I render this template within a sprockets context?</strong></p>