简体   繁体   English

如何在 Rails 3.1 中管理 CSS 样式表资产?

[英]How to manage CSS Stylesheet Assets in Rails 3.1?

I'm just learning the new asset pipeline in Rails 3.1.我只是在学习 Rails 3.1 中的新资产管道。 One particular problem I'm having is with the way Sprockets just mashes all the found CSS stylesheets into one massive stylesheet.我遇到的一个特殊问题是 Sprockets 只是将所有找到的 CSS 样式表混合成一个庞大的样式表。 I understand why this is advantageous over manually merging stylesheets and minifying for production.我理解为什么这比手动合并样式表和缩小生产更有利。 But I want to be able to selectively cascade stylesheets instead of having all rules all mashed together.但我希望能够有选择地级联样式表,而不是将所有规则混在一起。 For instance, I want:例如,我想要:

master.css主控.css

to be loaded by all pages in the Rails app, but I want由 Rails 应用程序中的所有页面加载,但我想要

admin.css only to be loaded by pages/views within the admin section/namespace. admin.css 只能由管理部分/命名空间中的页面/视图加载。

How can I take advantage of the great way that Rails 3.1 combines stylesheets and minifies them for production, but also have the former flexibility of being able to load only certain stylesheet combinations per layout?我怎样才能利用 Rails 3.1 结合样式表并将它们缩小以用于生产的出色方式,同时又具有以前的灵活性,即每个布局只能加载某些样式表组合?

Or should this be done by adding a class to body tags in layouts-或者这是否应该通过在布局中的正文标签中添加 class 来完成 -

body class="admin"身体类="管理员"

And then target style rules as appropriate.然后根据需要定位样式规则。 Using SASS scoped selectors this might be a reasonable solution.使用 SASS 范围选择器,这可能是一个合理的解决方案。

This is how i solved the styling issue: (excuse the Haml)这就是我解决样式问题的方法:(请原谅 Haml)

%div{:id => "#{params[:controller].parameterize} #{params[:view]}"}
    = yield

This way i start all the page specific .css.sass files with:这样我开始所有页面特定的.css.sass文件:

#post
  /* Controller specific code here */
  &#index
    /* View specific code here */
  &#new
  &#edit
  &#show

This way you can easily avoid any clashes.这样您就可以轻松避免任何冲突。

Hope this helped some.希望这对一些人有所帮助。

I have a post about this on my website: Leveraging Rails 3.1, SCSS, and the assets pipeline to differentiate your stylesheets我在我的网站上有一篇关于此的文章: 利用 Rails 3.1、SCSS 和资产管道来区分您的样式表

And check out this answer to another question: Using Rails 3.1 assets pipeline to conditionally use certain css并查看另一个问题的答案: Using Rails 3.1 assets pipeline to conditionally use certain css

Hope this helps.希望这可以帮助。

Best regards, Lasse最好的问候, 拉斯

@nathanvda: sure... @nathanvda:当然……

We make use of multiple layout files.我们使用多个布局文件。 So in our app/views/layouts, instead of having just application.html.haml (we use HAML), we actually ignore the application layout and use 3 custom layouts:所以在我们的 app/views/layouts 中,我们实际上忽略了应用程序布局并使用了 3 个自定义布局,而不是只有 application.html.haml(我们使用 HAML):

admin.html.haml (admin section views only) admin.html.haml(仅限管理部分视图)

registered.html.haml (registered/signed in users views only)已注册.html.haml(仅在用户视图中注册/签名)

unregistered.html.haml (unregistered/unsigned in users views only) unregistered.html.haml(仅在用户视图中未注册/未签名)

So at the top of my admin.html.haml file I will have my stylesheet link tags to a separate admin.scss (we use SCSS) manifest.所以在我的 admin.html.haml 文件的顶部,我将把我的样式表链接标签指向一个单独的 admin.scss(我们使用 SCSS)清单。 That manifest will load any necessary sub-stylesheets just for the admin section.该清单将为管理部分加载任何必要的子样式表。 This allows us to specify rules just for the admin section while also making use of common styles.这允许我们只为管理部分指定规则,同时还可以使用常见的 styles。 For instance, we use jquery-ui throughout the site, so the styles associated with jquery-ui sit in their own stylesheet and we include them in the manifests for all 3 css manifest files. For instance, we use jquery-ui throughout the site, so the styles associated with jquery-ui sit in their own stylesheet and we include them in the manifests for all 3 css manifest files.

This solution doesn't give you a single CSS file that can be cached, but it ends up giving you 3 CSS files, each of which can be cached.此解决方案不会为您提供可以缓存的单个 CSS 文件,但最终会为您提供 3 个 CSS 文件,每个文件都可以缓存。 This allows a tradeoff between performance and some flexibility in organizing CSS rules so we don't have to worry about CSS rule collisions.这允许在组织 CSS 规则时在性能和一些灵活性之间进行权衡,因此我们不必担心 CSS 规则冲突。

The way I've been doing it so far is to have two seperate folders a/ and u/ where a/ is for the admin view and u/ is for the user view.到目前为止,我一直在这样做的方式是有两个单独的文件夹 a/ 和 u/ 其中 a/ 用于管理视图, u/ 用于用户视图。 Then in the layout I point to the appropriate application.css with assets/u/application.css(js).然后在布局中,我指向相应的 application.css 和 assets/u/application.css(js)。 Bit of a pain having to move the auto generated files each time but a lot less than having to require each file individually in the manifest.每次都必须移动自动生成的文件有点痛苦,但比必须在清单中单独要求每个文件要少得多。

I use something like我使用类似的东西

application.html.erb ">应用程序.html.erb ">

show.html.erb显示.html.erb

content_for:body_id do page_specific_body_id end content_for:body_id 做 page_specific_body_id 结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM