OK, so after a short discussion and some experiments, I've realized that the Typo approach to themes has a lot more merit than I initially thought... For general use it still has some trouble areas, but that's nothing that can't be fixed.
I've created a new theme_generator that takes their general approach and makes it easy to leverage in any application. Even an existing one.
How to use it...
Usage is extremely easy. First, use the generator to create a default theme.
./script/generate theme default
(It doesn't have to be called 'default', it can be whatever you'd like)
This will create the main themes folder structure, the initial theme files for the theme ('default' in this case), and it creates the plugins/components needed to support themes.
Now you can use themes as easily as you do layouts. The following will use a layout named 'main' in the 'default' theme.
[code lang="ruby"]
class ApplicationController < ActionController::Base
layout 'main'
theme 'default'
end
[/code]
Here's an example of per-user themes:
[code lang="ruby"]
class ApplicationController < ActionController::Base
layout 'default'
theme :get_user_theme
def get_user_theme
# This assumes, of course, your User has a 'theme' field
return @session[:user].nil? ? 'default' : @session[:user].theme
end
end
[/code]
Differences from Typo themes...
I did take the liberty of tweaking of couple of things (beyond setting the theme in your controller). The main difference is that I changed the cached theme structure.
Before:
/public/images/theme/stylesheets/theme/javascript/theme
After:
/public/themes/[THEME_NAME]/images/stylesheets/javascript
What does this mean? For one thing, it's a lot easier to remove cached theme files... Just delete the public/themes folder.
Also, you should be using relative paths to images in your CSS. For example:
[code lang="css"]
BODY {
background: #FFF url(../images/page-bg.jpg) top left;
}
[/code]
Requirements...
The theme_generator requires Rails >= 0.14. The easiest way to install the theme_generator is by running:
sudo gem install theme_generator
It's that simple. Feel free to install it and let me know what you think.
As an extreme example -- you have your pretty, spiffy, AJAXified version... and then an "ASCII-alike" version for all (none) of those Lynx users :)
You would, of course, have to make sure your controllers aren't hardcoded for posts and/or ajax calls. They'd need to be flexible enough to handle both. Which, with a little forethought, shouldn't be a big deal.
Is this a limitation of Rails or a bug in the theme system? Or perhaps it is meant to be this way so it can fall back to the original files if no themes are installed? Maybe even just an error on my side.
Any insight on this is appreciated.
By the way, great work.
Also, I wonder if components are themable...
I tried doing a few variants of , but they all ended up as broken links. As soon as I moved them out of the subdir they worked just perfectly. Other than this, the theme generator works just great :)
can this system run with shared partials?
i would like to have a bunch of partials that represent formatted data that i could use in each of the themes..
forms, news blocks, and that kinda thing..
any ideas?
thanks!
Leave a comment...