I'm tyring to use the GEM css_views to generate dynamic CSS. The GEM can be found here: https://github.com/rhulse/rails-css-views
I've tried to follow the Readme, but removing the c开发者_运维百科ache/minify things. However, I don't understand what parameters I'm supposed to give to "packaged_stylesheet_path" and "css_configuration".
Here is my code:
# app/controllers/style_controllers.rb
require 'css_views'
class StylesheetsController < ApplicationController
include CssViews::ControllerMixins
before_filter :set_vars
css_configuration 'application', :components => ['user_css']
private
def set_vars
@corp_black = '#310C04'
end
end
-
#routes.rb
match '/stylesheets/:configuration_name(.:format)' => "stylesheets#show", :as=>:packaged_stylesheet
-
#app/views/layouts/application.html.erb
<head>
<%= packaged_stylesheet_path('user_css', :css) %>
</head>
What 'im trying to do is use view/stylesheets/user_css.css.erb with variables.
Thanks for any tips :)
Looking into source code i see that you just need to pass an empty array as :transformers
parameter
css_configuration 'application', :components => ['user_css'], :transformers => []
Thank you vooD for taking some time for helping me!
However, I've been lucky because I emailed the creator of css_views GEM and surprisingly, I did get a pretty fast answer. Very nice of him! I'll share it with you since it answers my question (saying it won't provide a solution to my problem).
In the application layout you would use this:
production_stylesheet_path( 'application', '2011033101', :css )
The first param is what you want the name of the file to be, the second is a string for cache busting and the last is the file type. The above will generate:
/stylesheets/application-2011033101.css
'application' is which css_configuration to use. So the one above uses this one from my stylesheets controller:
css_configuration "application", :components=>['global', 'application', 'recipes', 'print'],:transformers=>[Transformer.new]
The components refer to the name of files in the /views/stylesheets folder, and the order to compile them in.
In development it'll serve up the files dynamically, but in production it will write out one file into /public/stylesheets/application-2011033101.css
This means that you can have variables in the css.erb files BUT you cannot change them on a per request basis.
If you update anything in the CSS erb files you have to bump the number, otherwise the changes may not appear, especially if you have far-future dates set for the headers on the CSS.
The Gem has been overtaken somewhat by stuff coming in Rails 3.1 - they are integrating SASS - but if you want a lighter weight solution, this is it!
I hope that all makes sense - let me know if you have any further questions!
Cheers,
Richard
精彩评论