I’m in the process of building my first solo Rails app using Rails 3.1.rc5. My problem is that I want to have my site render the various CSS files conditionally. I’m using Blueprint CSS and I’m trying to have sprockets/rails render screen.css
most of the time, print.css
only when printing, and ie.css
only when the site is accessed from Internet Explorer.
Unfortunately, the default *= require_tree
command in the application.css
manifest includes everything in the assets/stylesheets
directory and results in an unpleasant CSS jumble. My current workaround is a sort of brute-force method where I specify everything individually:
In application.css:
*= require_self
*= require home.css
...
*= require blueprint/screen.css
In my stylesheets partial (haml):
<!--[if lt IE 9]
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
![endif]-->
= stylesheet_link_tag "application"
= stylesheet_link_tag 'blueprint/print', media: 'print'
<!--[if lt IE8]]
= stylesheet_link_tag 'blueprint/ie'
![endif]-->
= javascript_include_tag "application"
This works but it’s not especially pretty. I’ve done a few hours of searching to even get this far but I’m hoping that there’s some easier way to do it that I’ve just missed. If I could even selectively render certain directories (without including subdirectories) it would make the whole process a lot less rigid.
Thanks!
Came across this problem today.
Ended up putting all IE specific stylesheets into lib/assets/stylesheets and creating one manifest file per version of IE. Then in application.rb add them to the list of things to precompile :
And in your layouts, conditionally include those manifest files and you're good to go!
Thats a pretty neat way to do it. I use conditional classes on html or modernizr. See this article for a good representation on what does what. modernizr-vs-conditional-classes-on-html
I've discovered a way to make it less rigid and future proof by still using the asset pipeline but having the stylesheets grouped. It's not much simpler than your solution, but this solution allows you to automatically add new stylesheets without having to re-edit the whole structure again.
What you want to do is use separate manifest files to break things up. First you have to re-organize your
app/assets/stylesheets
folder:Then you edit the three manifest files:
Next you update your application layout file:
Lastly, don't forget to include these new manifest files in your config/environments/production.rb:
Update:
As Max pointed out, if you follow this structure you have to be mindful of image references. You have a few choices:
background: url('/assets/image.png');
background: image-url('image.png');