Skip to content

Instantly share code, notes, and snippets.

@rthbound
Forked from mattsmith/dynamic_stylesheet.rb
Last active August 29, 2015 09:03
Show Gist options
  • Save rthbound/f1fb0e57be93e9ab9fc2 to your computer and use it in GitHub Desktop.
Save rthbound/f1fb0e57be93e9ab9fc2 to your computer and use it in GitHub Desktop.
Renders a scss stylesheet with erb using Tilt templates.
class DynamicStylesheet
# logical_path = 'app/assets/stylesheets/custom.css.scss.erb'
def render(logical_path, data={})
path = Rails.root.join(logical_path)
context = env.context_class.new(env, logical_path, Pathname.new(path))
# TODO Change to Tilt.templates_for(file) in Tilt 2.x
templates = [Tilt::ERBTemplate, Sass::Rails::ScssTemplate]
css = templates.inject(nil) do |data, template|
blk = proc { data } if data
template.new(path.to_s, &blk).render(context, data)
end
end
def env
::YourApp::Application.assets
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment