Created
June 12, 2013 11:16
-
-
Save juno/5764461 to your computer and use it in GitHub Desktop.
Rails 3.2でコントローラーアクションの実行時に動的にSassテンプレートをCSS化する方法。 template.sass.erb => template.sass => CSS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StylesController | |
def show | |
# Local variables assign to ERb | |
locals = { | |
base_color: current_user.base_color | |
} | |
# Render ERb to Sass (app/views/styles/template.sass.erb) | |
sass_contents = self.class.new.render_to_string('template', | |
formats: [:sass], | |
layout: false, | |
locals: locals) | |
# Sass load paths | |
load_paths = ['.'] | |
# add path to Compass (installed via Bundler) | |
load_paths << "#{Gem.loaded_specs['compass'].full_gem_path}/frameworks/compass/stylesheets" | |
# add path to application local stylesheets | |
load_paths << Rails.root.join('app', 'assets', 'stylesheets') | |
# Compile Sass to CSS | |
css_contents = Sass.compile(sass_contents, | |
syntax: :sass, | |
load_paths: load_paths) | |
logger.debug "*** css = #{css_contents}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compass | |
@import compass/reset | |
@import compass/utilities | |
@import compass/css3 | |
// app/assets/stylesheets/shared/fonts | |
@import shared/fonts | |
body | |
$base-color: <%= base_color || '#fff' %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment