CSS switcher in Ruby?

Discussion in 'Ruby' started by Tom Cloyd, Oct 5, 2005.

  1. Tom Cloyd

    Tom Cloyd Guest

    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 / >>
    =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/
    Tom Cloyd, Oct 5, 2005
    #1
    1. Advertising

  2. Tom Cloyd

    James Britt Guest

    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
    James Britt, Oct 5, 2005
    #2
    1. Advertising

  3. ------=_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--
    Lyndon Samson, Oct 5, 2005
    #3
  4. Tom Cloyd

    Tom Cloyd Guest

    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 <> =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=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 / >>
    =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/
    Tom Cloyd, Oct 5, 2005
    #4
  5. Tom Cloyd

    James Britt Guest

    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/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
    James Britt, Oct 5, 2005
    #5
  6. Tom Cloyd

    James Britt Guest

    James Britt, Oct 5, 2005
    #6
  7. Tom Cloyd

    Hugh Sasse Guest

    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
    Hugh Sasse, Oct 5, 2005
    #7
  8. 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).
    Gavin Kistner, Oct 5, 2005
    #8
  9. Tom Cloyd

    Tom Cloyd Guest

    Gavin,

    Thanks for your contribution to this thread.

    On Wed, 05 Oct 2005 05:11:34 -0700, Gavin Kistner <> =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=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 / >>
    =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/
    Tom Cloyd, Oct 5, 2005
    #9
  10. Tom Cloyd

    Kirk Haines Guest

    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
    Kirk Haines, Oct 5, 2005
    #10
  11. 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
    > <> 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 / >>
    > ======================================================
    >
    > Using Opera's revolutionary e-mail client (program): http://
    > www.opera.com/mail/
    >
    >
    Adam Van Den Hoven, Oct 5, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank J. Reashore

    ASP.net Version Switcher

    Frank J. Reashore, Aug 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    405
    Craig Deelsnyder
    Aug 7, 2004
  2. Hardeep Rakhra
    Replies:
    8
    Views:
    641
    Hardeep Rakhra
    Jan 15, 2004
  3. Hardeep Rakhra
    Replies:
    11
    Views:
    590
    Jeffrey Silverman
    Jan 16, 2004
  4. Kerberos

    Nice links switcher - How is it called?

    Kerberos, Jan 10, 2005, in forum: Javascript
    Replies:
    1
    Views:
    88
    Robert
    Jan 12, 2005
  5. bedges
    Replies:
    0
    Views:
    88
    bedges
    Mar 22, 2006
Loading...

Share This Page