Dynamic CSS

Hey all,

I have a scenario where I am loading objects to display on the
screen, and they each have colors in the Model. I'd like to use these
hex values to generate CSS code so I can display them cleanly, any
suggestions?

I saw the rcss gem and it looks like that might work, but in order to
reduce the DB hit, I'd have to have a seperate CSS file that gets
called *only* when a jack is being displayed so the RCSS isn't calling
the DB each time to create the CSS for the jack colors, am I right?

I have a scenario where I am loading objects to display on the

screen, and they each have colors in the Model. I’d like to use these

hex values to generate CSS code so I can display them cleanly, any

suggestions?

I saw the rcss gem and it looks like that might work, but in order to

reduce the DB hit, I’d have to have a seperate CSS file that gets

called only when a jack is being displayed so the RCSS isn’t calling

the DB each time to create the CSS for the jack colors, am I right?

Why do it by generating a new css file? You can just add ‘style=“color: #mycolor”’ to the elements that need dynamic coloring. You can then use the rails caching to lighten the load.

Best regards

Peter De Berdt

It’s not very good practice to put styles within HTML, assuming this is what you were getting at.

Sure, I totally agree, but as I said before, if it’s for the better, you sometimes need to bend the rules a bit. Generating dynamic css will have the browser reload the whole css every time around instead of using the locally cached version. Not really very efficient, as 95% of the css file will be identical.

Now you have two options: split up your css file in a static part (that can be cached) and a dynamic one (which will generate another request to the server each time around) or just use styling in your XHTML file. I tend to favor the last option, because it doesn’t need yet another plugin. Also remember that if you’re planning to use scriptaculous visual effects, you’ll have to use the style attribute anyway (display: none;).

Like always, extremism leads nowhere :slight_smile:

Best regards

Peter De Berdt