在大型 Rails 项目中管理 CSS
在大型 Rails 项目中处理 CSS 的好方法有哪些?理想情况下,我希望能够在每个部分中包含 CSS 或 CSS 文件的链接。
我尝试过使用 content_for 和 Yields,它允许我将 CSS 插入到页面头部,其中的部分位于其他地方,但是某些部分被多次使用,这会导致样式链接加倍。
我理想的解决方案是能够为插入头部的每个部分添加一个样式表链接标签,然后在生产中这些链接将被整理成一个仅包含一次的大样式表。
What are some good methods for handling CSS in large Rails projects? Ideally I'd like to be able to include CSS or a link to a CSS file per partial.
I've played around with using content_for and yields which allows me to insert CSS into the head of the page with partials that are located elsewhere, however some partials get used more than once which would result in a double up of style links.
My ideal solution would be the ability to have a stylesheet link tag per partial that is inserted into the head, then in production these links will be collated into one big stylesheet that is only included once.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该考虑查看 Sass 和 指南针。 Sass 为您提供了一种生成 CSS 的绝妙方法。 Compass 为您提供了一个框架来更轻松地管理所有 Sass 样式表和 mixin。
You should consider looking into Sass and Compass. Sass gives you a brilliant way to generate CSS. Compass gives you a framework to manage all your Sass stylesheets and mixins more easily.
Compass 是一个很棒的库,但我更喜欢更简单的解决方案。就像 Samuel 提到的,这是矫枉过正。但是,我确实认为 Sass(尤其是 3.0)值得学习。 Mixins、变量、函数都是 CSS 应该具备的东西 :)
我部署到 Heroku,这使得将样式表编译到磁盘变得很棘手。所以我写了一个简单的解决方法,我在这里描述:
http://avandamiri.com/2010/09/15/managing-styles-with-sass-on-heroku.html
技巧是让服务器根据请求编译它们,然后使用 Varnish 缓存结果。我希望它有帮助。
Compass is a great library, but I prefer much more minimal solutions. Like Samuel, mentioned, it is overkill. But, I do think Sass (especially 3.0) is worth getting into. Mixins, variables, functions are all things CSS should have :)
I deploy to Heroku, which makes compiling stylesheets to disk tricky. So I wrote a simple workaround which I describe here:
http://avandamiri.com/2010/09/15/managing-styles-with-sass-on-heroku.html
The trick is to have the server compile them on request and then cache result with Varnish. I hope it helps.