Dynamic Stylsheets

Hello,

  Can anyone provide guidance on whether or not there is an idiomatic
way to create a very small set (say, <= 10 declarations) of dynamic CSS
with RoR?

  Method 1: Embedding a `style` tag into the page (whether directly or
rendered from a partial) in application.html.erb. This implies that the
"stylesheet" is either spaghetti'd into application.html.erb (which is
messier and more brittle), or is an actual partial living in
`app/views/layouts/` (which violates the Principle of Least
Astonishment). IMO this is probably the most practical solution
(balancing maintenance effort with runtime performance) but smells to
high-heaven.

  Method 2: Linking to a second, dynamic, stylesheet (dynamic.css.erb)
after application.css. Clean and clear-cut, but the additional GET,
especially for _such_ little content, seems ridiculous.

  Method 3: Morphing application.css into application.css.erb, using
require_self, and appending the dynamic styles into that file. Very
"direct" intent, but this would require the *entire* stylesheet manifest
to be preprocessed on every request - no bueno.

  Which is considered idiomatic? Perhaps there are other methods I've
not listed? Is there a section of the Asset Pipeline manual that I've
missed which addresses this specific question?

Cheers,
-Chris

Hello,

Can anyone provide guidance on whether or not there is an idiomatic
way to create a very small set (say, <= 10 declarations) of dynamic CSS
with RoR?

Method 1: Embedding a `style` tag into the page (whether directly or
rendered from a partial) in application.html.erb. This implies that the
"stylesheet" is either spaghetti'd into application.html.erb (which is
messier and more brittle), or is an actual partial living in
`app/views/layouts/` (which violates the Principle of Least
Astonishment). IMO this is probably the most practical solution
(balancing maintenance effort with runtime performance) but smells to
high-heaven.

Method 2: Linking to a second, dynamic, stylesheet (dynamic.css.erb)
after application.css. Clean and clear-cut, but the additional GET,
especially for _such_ little content, seems ridiculous.

Method 3: Morphing application.css into application.css.erb, using
require_self, and appending the dynamic styles into that file. Very
"direct" intent, but this would require the *entire* stylesheet manifest
to be preprocessed on every request - no bueno.

Which is considered idiomatic? Perhaps there are other methods I've
not listed? Is there a section of the Asset Pipeline manual that I've
missed which addresses this specific question?

Cheers,
-Chris

I just encountered this pattern in a Rails Composer app that I'm working on, and it's working quite well for what I'm doing here.

  <body class="<%= controller_name %> <%= action_name %>">

By using these keys, I can create quite literally any sort of site segment-specific style that I need, and do it once in the compiled CSS. Yes there are more lines of CSS for everyone to download, but the file will be cached after the first page, so there's little or no downside.

  #whatever.pages { ... }
  #whatever.pages.index { ... }
  #whatever.pages.show { ... }

Unless you are providing a service where the user can change settings (I like purple backgrounds or something) and you want to use the session to drive these personal choices, I imagine that you could get a lot of mileage out of this sort of approach.

Walter