I have no idea if this is a goofy idea or not (and I'm only just starting with Ruby and unable to attempt this project myself), but might a CSS switcher for web pages be possible in Ruby? Seems quite possible to me, but...
I have no idea if this is a goofy idea or not (and I'm only just starting with Ruby and unable to attempt this project myself), but might a CSS switcher for web pages be possible in Ruby? Seems quite possible to me, but...
What might this do? CSS switchers I'm familiar with use JavaScript to rewrite the DOM and change the selected CSS in the browser:
Having some server code emit different CSS, or change the CSS link specified in some Ruby-generated HTML page, seem trivial.
One might, I suppose, combine these things and use remote scripting to fetch a custom CSS file or modify inline CSS in the browser.
I have no idea if this is a goofy idea or not (and I'm only just starting with Ruby and unable to attempt this project myself), but might a CSS switcher for web pages be possible in Ruby? Seems quite possible to me, but...
Just in case it shot past unnoticed: you can have alternative style
sheets. There's probably a reason you don't want them here, but I
thought I'd flag that up...
Possible, and generally easy, though the details depend a great deal on what
environment one is running one's web stuff in.
I have two approaches.
One approach is for occasions where there may be both static and non-static
pages that all need to have some intelligent stylesheet selection. Often I
use this just to have a special stylesheet for browsers that say they are IE,
so that I don't have to deal with gross box model hacks and other pain
involved in making a single stylesheet work with, say, IE and Firefox.
A version of the code I use for this style would look something like this:
def styles
session.context.request.content_type = 'text/css'
begin
headers = session.context.request.headers_in['User-Agent']
# A very naive browser type check follows.
style_file = header =~ /MSIE/ ? 'styles.css' : 'styles_nonie.css'
if File.stat(style_file).mtime.to_i > @@styles_mtimes[style_file]
@@styles[style_file] = File.read(style_file)
@@styles_mtimes[style_file] = File.stat(style_file).mtime.to_i
end
rescue Exception
# Something bad happened. Do something?
end
@@styles[style_file]
end
end
A variation of this could easily be used to select a stylesheet based on a
cookie value, for more sophisticated CSS file switching.
The other approach that I use occasionally is to embed the logic at the other
end, in the code that generates the original HTML page.
css_url would be a method that would return the URL to the desired CSS file.
One obvious problem with this method is that it requires that the HTML page
be dynamically generated. Having a smart /styles.css delivery, on the other
hand, means that even static HTML files can be served different stylesheets
based on browser type or a cookie or some other trait, and that's how I
usually approach this issue. The browser never knows that the response from
a request for /styles.css is being dynamically generated.
Kirk Haines
···
On Tuesday 04 October 2005 10:17 pm, Tom Cloyd wrote:
I have no idea if this is a goofy idea or not (and I'm only just starting
with Ruby and unable to attempt this project myself), but might a CSS
switcher for web pages be possible in Ruby? Seems quite possible to me,
but...
What I had in mind was to do what the fairly common PHP server-side CSS switcher does, only with Ruby. There are advantages to a server-side solution, and if someone coded it in Ruby I'd probably understand it someday. I hope never to have to understand (i.e., study) PHP. I have a life outside of computers, and that leaves very little free time. So...hoping for an all-Ruby little world I can live in.
I agree that it seems trivial...but not for me. Someone else, maybe?
Here's the PHP switcher article on A List Apart, for example...
- t.
···
On Tue, 04 Oct 2005 21:39:19 -0700, James Britt <james_b@neurogami.com> wrote:
Tom Cloyd wrote:
I have no idea if this is a goofy idea or not (and I'm only just starting with Ruby and unable to attempt this project myself), but might a CSS switcher for web pages be possible in Ruby? Seems quite possible to me, but...
What might this do? CSS switchers I'm familiar with use JavaScript to rewrite the DOM and change the selected CSS in the browser:
What I had in mind was to do what the fairly common PHP server-side CSS switcher does, only with Ruby. There are advantages to a server-side solution, and if someone coded it in Ruby I'd probably understand it someday. I hope never to have to understand (i.e., study) PHP. I have a life outside of computers, and that leaves very little free time. So...hoping for an all-Ruby little world I can live in.
I agree that it seems trivial...but not for me. Someone else, maybe?
Here's the PHP switcher article on A List Apart, for example...
Yes, that would make a decent project for getting into Ruby.
Off the top of my head:
Create dynamic_css.rb (or whatever you call it)
Make it:
Read a cookie to see if there are any current values to apply
Check for the presence of any querystring to see if
there are new values to handle.
Use those values in building some CSS.
Return the CSS text, with the correct content-type header.
And set a new cookie, too, for the next request
FWIW - creating dynamic content breaks browser caching. (This should be obvious, but I thought I'd say it anyhow.) If your HTML is static and huge and your CSS small and dynamic, then creating CSS on the fly makes sense.
However, if you have a couple static 'theme' CSS files and your HTML is already dynamic, users will save bandwidth and time if your HTML generator rewrites the <link...> tag on the fly to pre-existing, static CSS files. This way, the browser can cache the CSS file (provided you also set up your web server to send caching HTTP headers).
FWIW - creating dynamic content breaks browser caching. (This should be obvious, but I thought I'd say it anyhow.)
Not everyone here is whiz. I'm out of my field here, and a distinct amateur. I work at it really hard but you might be surprized as to what's not obvious to me (and to some others here, I expect), so your calling attention to this is actually quite helpful.
Now...I'm putting your comments in a note in my file on this little project, so I can study it in the near future, since at first read, very little of it is obvious to me!
Thanks again, truly.
-- t.
···
On Wed, 05 Oct 2005 05:11:34 -0700, Gavin Kistner <gavin@refinery.com> wrote:
======================================================
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< BestMindHealth.com / tc@bestmindhealth.com >>
Actually there is a way to have dynamic_css.rb without breaking browser caching! I've never actually done this but from my understanding of HTTP, it should work.
The trick is to not change the URL for each style, rather change something else like a cookie (if you want it to persist between visits) or a session variable(assuming you have access to one). I'm going to assume that you have figured out some way to set the cookie and have the css load itself (keeping in mind that no everyone has javascript enabled).
What you need to do is leverage the Entity tags and If-None-Match headers.
Lets say you have two styles Style1 and Style2. When you get a request to dynamic_css.rb and your STYLE cookie is set to to Style1 (or nothing), send your CSS for Style1. With this response add the header
ETag: "Style1"
Now every subsequent request will include
If-None-Match: "Style1"
Lets say that our cookie changes to Style2
Now we send the header
ETag: "Style2"
From this point on, all requests will come with:
If-None-Match: "Style1", "Style2"
Now here comes the magic. All you do is instead of sending your content with a 200 - OK response, send a 304 - Not Modified response and include the ETag header of the CSS you want to be used.
The only thing then is how do you handle the case when the css changes. For this you can still use the If-Modified-Since header to compare with the last modified date of the rb file
···
On 5-Oct-05, at 12:53 PM, Tom Cloyd wrote:
Gavin,
Thanks for your contribution to this thread.
On Wed, 05 Oct 2005 05:11:34 -0700, Gavin Kistner > <gavin@refinery.com> wrote:
Create dynamic_css.rb (or whatever you call it)
FWIW - creating dynamic content breaks browser caching. (This should be obvious, but I thought I'd say it anyhow.)
Not everyone here is whiz. I'm out of my field here, and a distinct amateur. I work at it really hard but you might be surprized as to what's not obvious to me (and to some others here, I expect), so your calling attention to this is actually quite helpful.
Now...I'm putting your comments in a note in my file on this little project, so I can study it in the near future, since at first read, very little of it is obvious to me!
Thanks again, truly.
-- t.
======================================================
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< BestMindHealth.com / tc@bestmindhealth.com >>