views:

82

answers:

2

What is the best way to handle style that that is user-customized? Just as an example of the result I'm looking for, this would suffice:

body {
   color: {{ user.profile.text_color }};
}

However, serving CSS as a view seems like it would cause a significant amount of overhead in a file that is constantly requested, so this is probably not a good solution.

The user does not have access to the CSS files and we must assume that they have no web development knowledge.

+2  A: 

Do the CSS dynamically via a view as normal, but use aggressive caching so that it loads quickly.

Ignacio Vazquez-Abrams
+1 for KISS....
Ofri Raviv
I concur with the KISS sentiment, this seems sufficient.
cloudshao
This solution is a bit simplistic. The CSS is on a *per user* basis and to cache properly that means a different cache item for each possible setting. An alternative is to cache 98% of the page, but use a dynamic outer-wrapper `<div class="{{style_choice}}"> ... whatever ... </div>` that uses the per-user setting. Alternatively, you could use JavaScript to tweak the outer class, but that may not be acceptable in this situation.
Peter Rowell
+4  A: 

However, serving CSS as a view seems like it would cause a significant amount of overhead in a file that is constantly requested, so this is probably not a good solution.

And what if you would generate that CSS once?

  1. Default CSS is: /common/css.css
  2. Member customize CSS, now <link /> elements points to /user-specific/123.css?ts=123123123. 123 is of course an identifier of the member, and ts parameter contains a timestamp - a date of last CSS modification
  3. Make sure that your CSS generator sets proper HTTP headers responsible for client-side caching
  4. User browser request a CSS file - server replies whit simple 304 Not Modified header - there is no need for any script execution or contents download
  5. When member modifies his CSS then you just update ts - once again just a single request is needed
Crozin
+1 Makes sense to me.
Manoj Govindan