使用Rails 3.1资产管道有条件地使用某些CSS
我正在使用 Rails 3.1.rc5 构建我的第一个单独的 Rails 应用程序。我的问题是我想让我的网站有条件地渲染各种 CSS 文件。我正在使用 Blueprint CSS,并且我尝试让 sprockets/rails 大部分时间渲染 screen.css
,仅在打印时渲染 print.css
,并且 仅当从 Internet Explorer 访问站点时才使用 ie.css
。
不幸的是,application.css
清单中的默认 *= require_tree
命令包含 assets/stylesheets
目录中的所有内容,并导致令人不快的 CSS 混乱。我当前的解决方法是一种暴力方法,我单独指定所有内容:
在 application.css 中:
*= require_self
*= require home.css
...
*= require blueprint/screen.css
在我的样式表部分(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"
这可行,但不是特别漂亮。我已经做了几个小时的搜索才能走到这一步,但我希望有一些我刚刚错过的更简单的方法。如果我什至可以有选择地渲染某些目录(不包括子目录),那么整个过程就会变得不那么严格。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现了一种方法,通过仍然使用资产管道但对样式表进行分组,使其不那么严格且面向未来。它并不比您的解决方案简单多少,但该解决方案允许您自动添加新的样式表,而无需再次重新编辑整个结构。
您想要做的是使用单独的清单文件来分解事情。首先,您必须重新组织您的
app/assets/stylesheets
文件夹:然后编辑三个清单文件:
接下来更新您的应用程序布局文件:
最后,不要忘记包含这些新的清单文件在你的 config/environments/development.rb 中:
更新:
正如 Max 指出的,如果你遵循这个结构,你必须注意图像引用。您有几个选择:
背景:url('/assets/image.png');
背景:image-url('image.png');
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');
今天遇到这个问题。
最终将所有 IE 特定样式表放入 lib/assets/stylesheets 中,并为每个版本的 IE 创建一个清单文件。然后在 application.rb 中将它们添加到要预编译的内容列表中:
并在您的布局中,有条件地包含这些清单文件,然后就可以开始了!
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!
这是一个非常巧妙的方法。我在 html 或modernizr 上使用条件类。请参阅这篇文章,了解什么是做什么的。
modernizr-vs-conditional-classes-on-html< /a>
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