Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > CSS switcher in Ruby?

Reply
Thread Tools

CSS switcher in Ruby?

 
 
Tom Cloyd
Guest
Posts: n/a
 
      10-05-2005
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=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
Tom Cloyd
Bellingham, Washington, U.S.A: (360) 920-1226
<< BestMindHealth.com / http://www.velocityreviews.com/forums/(E-Mail Removed) >>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=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/


 
Reply With Quote
 
 
 
 
James Britt
Guest
Posts: n/a
 
      10-05-2005
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:

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


 
Reply With Quote
 
 
 
 
Lyndon Samson
Guest
Posts: n/a
 
      10-05-2005
------=_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...

--
Into RFID? www.rfidnewsupdate.com <http://www.rfidnewsupdate.com> Simple,
fast, news.

------=_Part_19878_32427261.1128488819888--


 
Reply With Quote
 
Tom Cloyd
Guest
Posts: n/a
 
      10-05-2005
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.

On Tue, 04 Oct 2005 21:39:19 -0700, James Britt <(E-Mail Removed)> =20
wrote:

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

>
> 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=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=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 Removed) >>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=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/


 
Reply With Quote
 
James Britt
Guest
Posts: n/a
 
      10-05-2005
Tom Cloyd wrote:
> 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/libdo...asses/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


 
Reply With Quote
 
James Britt
Guest
Posts: n/a
 
      10-05-2005
Lyndon Samson wrote:
> Sounds like a job for the abode of the mouse...
>


Not all are holed-up, though.


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


 
Reply With Quote
 
Hugh Sasse
Guest
Posts: n/a
 
      10-05-2005
On Wed, 5 Oct 2005, 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...


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


Hugh


 
Reply With Quote
 
Gavin Kistner
Guest
Posts: n/a
 
      10-05-2005
On Oct 4, 2005, at 11:46 PM, James Britt 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.) 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).


 
Reply With Quote
 
Tom Cloyd
Guest
Posts: n/a
 
      10-05-2005
Gavin,

Thanks for your contribution to this thread.

On Wed, 05 Oct 2005 05:11:34 -0700, Gavin Kistner <(E-Mail Removed)> =20
wrote:

>> Create dynamic_css.rb (or whatever you call it)

>
> 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=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=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 Removed) >>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=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/


 
Reply With Quote
 
Kirk Haines
Guest
Posts: n/a
 
      10-05-2005
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...


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
css stylesheet switcher and body background.... bedges Javascript 0 03-22-2006 11:59 AM
where can I find a connection switcher FireBrick Wireless Networking 3 08-27-2004 02:26 PM
ASP.net Version Switcher Frank J. Reashore ASP .Net 1 08-07-2004 07:09 PM
alistaprtstyle switcher, works but kills my default style :O( Hardeep Rakhra HTML 11 01-16-2004 04:50 PM
Javascript Style Switcher that remebers current site style in use Hardeep Rakhra HTML 8 01-15-2004 08:00 PM



Advertisments