What would be the best way to go about doing dynamic css with rails? I''m trying to do a theme kinda thing, where there''s multiple css files that can be loaded, but I want to make it so that users can change the color schemes for those themes. I don''t think I really want to embed the css style information into the html, I''d rather have it reside in an external css file. However, I''d also like to have all of the theme related files in one directory, not spread around, so creating the thing with multiple .rhtml files in the view directories is probably less than ideal. Any ideas for the best way to go about this? -- Bob Aman
I am looking at implementing this sort of scenario as well. I just started (last night) learning rails, but if I do find something soon, I''ll post it back to this thread. Just wanted to let you know you''re not alone :) On 5/7/05, Bob Aman <vacindak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> What would be the best way to go about doing dynamic css with rails? > I''m trying to do a theme kinda thing, where there''s multiple css files > that can be loaded, but I want to make it so that users can change the > color schemes for those themes. I don''t think I really want to embed > the css style information into the html, I''d rather have it reside in > an external css file. However, I''d also like to have all of the theme > related files in one directory, not spread around, so creating the > thing with multiple .rhtml files in the view directories is probably > less than ideal. Any ideas for the best way to go about this? > -- > Bob Aman > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Found this link which has been a good starting point for me: http://www.fivesevensix.com/articles/category/CSS On 5/7/05, Andy Stone <xsltguru-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am looking at implementing this sort of scenario as well. I just > started (last night) learning rails, but if I do find something soon, > I''ll post it back to this thread. Just wanted to let you know you''re > not alone :) > > On 5/7/05, Bob Aman <vacindak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > What would be the best way to go about doing dynamic css with rails? > > I''m trying to do a theme kinda thing, where there''s multiple css files > > that can be loaded, but I want to make it so that users can change the > > color schemes for those themes. I don''t think I really want to embed > > the css style information into the html, I''d rather have it reside in > > an external css file. However, I''d also like to have all of the theme > > related files in one directory, not spread around, so creating the > > thing with multiple .rhtml files in the view directories is probably > > less than ideal. Any ideas for the best way to go about this? > > -- > > Bob Aman > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
You can see one inplementation at http://csszengarden.com/. This isn''t anything Rails specific, but will hopefully give you an idea. As far as how to layout the files, create a template file app/views/layouts/application.rhtml. It should contain your the basic HTML framework that you want to appear on every page, including loading the CSS file, and the links to change the theme. the tag <%content_for_layout %> will be replaced by whatever html is in your <action>.rhtml file. Hopefully that helps... I''m VERY new at this myself, so please forgive me if it doesn''t work! On 5/7/05, Bob Aman <vacindak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> What would be the best way to go about doing dynamic css with rails? > I''m trying to do a theme kinda thing, where there''s multiple css files > that can be loaded, but I want to make it so that users can change the > color schemes for those themes. I don''t think I really want to embed > the css style information into the html, I''d rather have it reside in > an external css file. However, I''d also like to have all of the theme > related files in one directory, not spread around, so creating the > thing with multiple .rhtml files in the view directories is probably > less than ideal. Any ideas for the best way to go about this? > -- > Bob Aman > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 08/05/05, Mike Payson <mikepayson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You can see one inplementation at http://csszengarden.com/. This isn''t > anything Rails specific, but will hopefully give you an idea. > > As far as how to layout the files, create a template file > app/views/layouts/application.rhtml. It should contain your the basic > HTML framework that you want to appear on every page, including > loading the CSS file, and the links to change the theme. the tag <%> content_for_layout %> will be replaced by whatever html is in your > <action>.rhtml file. > > Hopefully that helps... I''m VERY new at this myself, so please forgive > me if it doesn''t work!Yeah, you top posted ;) Anyway, here''s another one: http://www.alistapart.com/articles/bodyswitchers/ IMO, stylesheets are a browser thing, so switching should be a browser action, not a server side change. -- Phillip Hutchings http://www.sitharus.com/ sitharus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org / sitharus-QrR4M9swfipWk0Htik3J/w@public.gmane.org
Here''s why CSS Zen Garden prefers a page reload... Why a page refresh when loading new styles? It''s tempting to think it could be handled with a Javascript style switcher instead of the current server-side method. There are two important reasons why it''s better not to: 1) you can bookmark each style this way. 2) the javascript method only adds to the existing style, instead of replacing it. A flush would be necessary to clear each one, and the simplest way to achieve this just happens to be a page refresh. I have no preference. You can always set a cookie to specify the preferred theme, so I don''t see that as a major problem either way. It is nice to save the roundtrip though. And yes, I top posted. You bottom posted. You should be proud of yourself.> Yeah, you top posted ;) > > Anyway, here''s another one: http://www.alistapart.com/articles/bodyswitchers/ > > IMO, stylesheets are a browser thing, so switching should be a browser > action, not a server side change.
On 08/05/05, Mike Payson <mikepayson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Here''s why CSS Zen Garden prefers a page reload... > Why a page refresh when loading new styles? > It''s tempting to think it could be handled with a Javascript style > switcher instead of the current server-side method. There are two > important reasons why it''s better not to: 1) you can bookmark each > style this way. 2) the javascript method only adds to the existing > style, instead of replacing it. A flush would be necessary to clear > each one, and the simplest way to achieve this just happens to be a > page refresh.Works fine for me when using Safari or FireFox, no matter what I do. It doesn''t just add to the current styles, otherwise the behaviour would be quite weird. However, IE is generally what screws these things up.> I have no preference. You can always set a cookie to specify the > preferred theme, so I don''t see that as a major problem either way. It > is nice to save the roundtrip though. > > And yes, I top posted. You bottom posted. You should be proud of yourself.It makes so much difference. -- Phillip Hutchings http://www.sitharus.com/ sitharus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org / sitharus-QrR4M9swfipWk0Htik3J/w@public.gmane.org
i have your solution I''m trying to do a theme kinda thing, where there''s multiple css files> that can be loaded, but I want to make it so that users can change the > color schemes for those themes.if you mean doing it backpack (backpackit.com <http://backpackit.com>) style, you should have the user visit a page to choose their preferred color scheme (or theme) and then have them submit their choice. I don''t think I really want to embed> the css style information into the html, I''d rather have it reside in > an external css file.give each color scheme css file a unique name and put it in your public/stylesheets directory (e.g. blueorange.css) store the users choice in the user table and make sure it gets included in the session. you should make a field called stylesheet where the default is the name of your default theme. when the user selects their stylesheet of choice, run an action to store the name of that stylesheet in the field. (e.g. the user selects "blue and orange" so you change the field to "blueorange") then you include a <%= stylesheet_link_tag "@session[:stylesheet]" %> in the layout. and it will load the users choice of stylesheet However, I''d also like to have all of the theme> related files in one directory, not spread around, so creating the > thing with multiple .rhtml files in the view directories is probably > less than ideal.That is the nature of MVC so that technically is NOT a good idea to do that. It makes it more difficult to create a packaged "theme" but its not too hard to separate them out. Any ideas for the best way to go about this? I''m not sure about how you''d go about creating variable views, I''ll be interested to see how you do it. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> > I don''t think I really want to embed > > the css style information into the html, I''d rather have it reside in > > an external css file. > > give each color scheme css file a unique name and put it in your > public/stylesheets directory (e.g. blueorange.css)Not going to work. Note the subject line: Dynamic CSS. The user is supposed to have complete control over the color scheme itself. There isn''t going to be a finite selection of colors to choose from. The user can set their foreground text color, background color, etc, etc, to be anything they want. To make matters more complicated, there are multiple css files for different themes (not the same as color schemes). It''s like skins in things like winamp, you select a theme, and then a color scheme for that theme, except that you can make the color scheme whatever you choose. So it''s more like: /* default_theme */ body { background-color: <%= theme_background_color or "#ffffff" %> } etc, etc. That make more sense now? I''m trying to figure out how to implement that. I could make those into .rhtml files and do some routing stuff. This is where I''m currently headed: In routes.rb: map.connect ''themes/:theme/screen.css'', :controller => ''theme'', :action => ''screen'' map.connect ''themes/:theme/print.css'', :controller => ''theme'', :action => ''print'' map.connect ''themes/:theme/handheld.css'', :controller => ''theme'', :action => ''handheld'' And then somehow the theme controller does some magic and hands back the appropriate dynamically generated css file. I''m trying to figure out what that magic is going to be. -- Bob Aman
* Bob Aman (vacindak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org) wrote:> And then somehow the theme controller does some magic and hands back > the appropriate dynamically generated css file. I''m trying to figure > out what that magic is going to be.Wouldn''t this be much easier to do with something like: <body style="<%= @user.style.body %>"> ? iain -- "If sharing a thing in no way diminishes it, it is not rightly owned if it is not shared." -- St. Augustine #rm -rf / http://www.geeksoc.org/
On 5/8/05, iain d broadfoot <ibroadfo-jbDEWmSD8rMwZN84zEGlPg@public.gmane.org> wrote:> * Bob Aman (vacindak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org) wrote: > > And then somehow the theme controller does some magic and hands back > > the appropriate dynamically generated css file. I''m trying to figure > > out what that magic is going to be. > > Wouldn''t this be much easier to do with something like: > > <body style="<%= @user.style.body %>"> > > ? > > iainMuch easier yes. And I would very much rather not. -- Bob Aman
Why don''t you just create a "stylesheet" method somewhere inside an appropriate controller, and then create an rhtml file that only contains css data (along with a few erb tags)? That way, you''ll have a method that outputs nothing but css to the client, dynamically. I''ve been doing the same thing with javascript a few times, and it works just fine. Just remember to send the correct Content-Type header (text/css) to the client. Otherwise it will be sent as text/ html, which browsers tend to complain about *cough*wiki.rubyonrails.com*cough* :) //samuel> > Much easier yes. And I would very much rather not. > -- > Bob Aman > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
As of now I''m going along this path: #app/controllers/application.rb #start paste class ApplicationController < ActionController::Base layout :site_layout attr_writer :site attr_reader :site before_filter {|c| parts = c.request.host.split(''.'') subdomain = parts.first if (subdomain == ''www''): subdomain = parts[1] end c.site = Site.find_by_subdomain(subdomain) } def site_layout site.subdomain+"_layout" end end #end paste I''m creating different layouts per site with the layouts residing in app/views/layout/<subdomain>_layout.rhtml Each layout will have it''s own css (or multiples), similar to what you stated above. If I understand what you are going after, you can define a set of templates (skins) and use this type of implementation to apply the template view to the site. I guess for the extra custom css you would then create an edit page for the user to customize the colors,fonts, or whatever. You could either store those values in the db and include the custom style in the page or create an external stylesheet (referenced in the db for the user) and fill in the appropriate link src="custom.css" tag. Just some thoughts, andy On 5/8/05, Bob Aman <vacindak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5/8/05, iain d broadfoot <ibroadfo-jbDEWmSD8rMwZN84zEGlPg@public.gmane.org> wrote: > > * Bob Aman (vacindak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org) wrote: > > > And then somehow the theme controller does some magic and hands back > > > the appropriate dynamically generated css file. I''m trying to figure > > > out what that magic is going to be. > > > > Wouldn''t this be much easier to do with something like: > > > > <body style="<%= @user.style.body %>"> > > > > ? > > > > iain > > Much easier yes. And I would very much rather not. > -- > Bob Aman > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
* Bob Aman (vacindak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org) wrote:> On 5/8/05, iain d broadfoot <ibroadfo-jbDEWmSD8rMwZN84zEGlPg@public.gmane.org> wrote: > > Wouldn''t this be much easier to do with something like: > > > > <body style="<%= @user.style.body %>"> > > > > ? > > > > iain > > Much easier yes. And I would very much rather not.Could you elaborate on your reasons? It''d be nice to know if my understanding of the best way to do things like this is flawed. :) cheers, iain -- "If sharing a thing in no way diminishes it, it is not rightly owned if it is not shared." -- St. Augustine #rm -rf / http://www.geeksoc.org/
> Could you elaborate on your reasons? It''d be nice to know if my > understanding of the best way to do things like this is flawed. > :) > > cheers, > iainSimple answer is perfectionism. I''ve always disliked mix-and-match. I don''t like embedding other things into html. I like my html being just html; no style blocks, no style attributes, no script blocks that don''t point to external .js files. In some cases, it''s easier and better to do it this way, in others, it''s just obstinence. I doubt you''re missing anything. This is not the easiest way to do things. There aren''t any advantages I know of other than satisfying my sense of aesthetic. Next question: Can I render_partial from an arbitrary file outside of where rails expects it to be? Normally when you do a render_partial, it looks for the .rhtml file in a specific place. Can I change where that place is? -- Bob Aman
On 08/05/2005, Andy Stone wrote : Hello,> class ApplicationController < ActionController::Base > layout :site_layout > > attr_writer :site > attr_reader :siteHere you can do attr_accessor :site. It''s a sort of shortcut for attr_writer + attr_reader. Have a good night ! -- Nicolas Cavigneaux | GPG KeyID : CFE76D24 nico-DRabjd/C3MEdnm+yROfE0A@public.gmane.org | http://www.bounga.org _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Alrighty, I figured out all my issues except one. Let''s say you have a public/themes/default/screen.css file. Let''s say you also have map.connect ''themes/:theme/screen.css'', :controller => ''theme'', :action => ''screen'' Both of those should respond to a request for public/themes/default/screen.css . As it turns out, the static file wins, and its contents are what are returned. I need it to lose. How do I make that happen? And can I make it lose for only the files within the public/themes directory tree? -- Bob Aman
> Both of those should respond to a request for > public/themes/default/screen.css . As it turns out, the static file > wins, and its contents are what are returned. I need it to lose. How > do I make that happen? And can I make it lose for only the files > within the public/themes directory tree?This depends on your redirection-rules. Normally the webserver only gives control to Rails, when there''s no file to serve at the given location. So a static file always wins if you don''t adapt the redirection rules specifically for the needed path. But then the dynamic file always wins and you have to render the contents of the static-file for yourself, something you can do easily if you just remove the static file from the public directory. So I see no advantage in doing so. Just serve the CSS-File dynamically and include the static content when needed. So you have full control about your output and don''t have to mess around with redirection-rules for the webserver.