CSS switcher in Ruby?

T

Tom Cloyd

I have no idea if this is a goofy idea or not (and I'm only just starting=
=20
with Ruby and unable to attempt this project myself), but might a CSS =20
switcher for web pages be possible in Ruby? Seems quite possible to me, =20
but...

-- t

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
Tom Cloyd
Bellingham, Washington, U.S.A: (360) 920-1226
<< BestMindHealth.com / (e-mail address removed) >>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D

Using Opera's revolutionary e-mail client (program): =20
http://www.opera.com/mail/
 
J

James Britt

Tom said:
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:

http://www.alistapart.com/articles/alternate/

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.



James

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
 
L

Lyndon Samson

------=_Part_19878_32427261.1128488819888
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Sounds like a job for the abode of the mouse...
 
T

Tom Cloyd

What I had in mind was to do what the fairly common PHP server-side CSS =20
switcher does, only with Ruby. There are advantages to a server-side =20
solution, and if someone coded it in Ruby I'd probably understand it =20
someday. I hope never to have to understand (i.e., study) PHP. I have a =20
life outside of computers, and that leaves very little free time. =20
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...

http://www.alistapart.com/articles/phpswitch/

- t.

What might this do? CSS switchers I'm familiar with use JavaScript to = =20
rewrite the DOM and change the selected CSS in the browser:

http://www.alistapart.com/articles/alternate/

Having some server code emit different CSS, or change the CSS link =20
specified in some Ruby-generated HTML page, seem trivial.

One might, I suppose, combine these things and use remote scripting to = =20
fetch a custom CSS file or modify inline CSS in the browser.



James



--=20

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< BestMindHealth.com / (e-mail address removed) >>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D

Using Opera's revolutionary e-mail client (program): =20
http://www.opera.com/mail/
 
J

James Britt

Tom said:
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...

http://www.alistapart.com/articles/phpswitch/

Ah.

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

Take a look at the Ruby CGI class

http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html


Ask questions here if you get stuck.


James

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
 
H

Hugh Sasse

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...

Hugh
 
G

Gavin Kistner

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.) 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).
 
T

Tom Cloyd

Gavin,

Thanks for your contribution to this thread.

FWIW - creating dynamic content breaks browser caching. (This should be= =20
obvious, but I thought I'd say it anyhow.)

Not everyone here is whiz. I'm out of my field here, and a distinct =20
amateur. I work at it really hard but you might be surprized as to what's=
=20
not obvious to me (and to some others here, I expect), so your calling =20
attention to this is actually quite helpful.

Now...I'm putting your comments in a note in my file on this little =20
project, so I can study it in the near future, since at first read, very =
=20
little of it is obvious to me!

Thanks again, truly.

-- t.

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< BestMindHealth.com / (e-mail address removed) >>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D

Using Opera's revolutionary e-mail client (program): =20
http://www.opera.com/mail/
 
K

Kirk Haines

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...

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:

class Styles < Iowa::Component
@@styles_mtimes = {'styles.css' => 0, 'styles_nonie.css' => 0}
@@styles = {}

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.

<link rel="stylesheet" type="text/css" href="@css_url" />

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
 
A

Adam Van Den Hoven

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top