I was pretty impressed after looking at the theme support in Typo 2.5. However, I don’t think it’s a system that you should replicate if you want your application to support simple themes.
Before I get too far along let me make it clear that it’s not my intention to cast aspersions on Typo, it’s an excellent tool. In fact, I plan on switching to it from WordPress in the near future.
My issue with Typo’s theme scheme (heh) is that it doesn’t leverage the web-server as it should. Rails is impressive, and it’s caching is quite good. But web-servers like Apache have been around for many years. They are really good at what they do, so let them do it! Rails can’t compete with the server’s ability to deliver static content like CSS and graphics.
Following is how I’ve been implementing themes in Rails applications. In addition to relying on the web-server to provide static files, it also leverages existing Rails infrastructure wherever possible. Bear in mind, I’m not suggesting the following to be an end-all solution. Nor is it a drop-in replacement for Typo’s theme support. Merely think of it as a good starting point.
I’ve been using this approach enough that I’ve created a generator for it.
Usage
After you’ve installed the generator, just call it with the name of the theme you’d like to create.
./script/generate theme blue-bird
It will then add theme_engine.rb to your lib/ folder and create the required file structure.
Note: On Windows, it looks more like:
ruby script\\generate theme blue-bird
File Structure
Let’s start by having a look at the file structure it creates. Here’s an example for a theme named ‘blue-bird’.
app/views/layouts/themes/blue-bird/public/themes/blue-bird/images/javascripts/theme.css
Just from looking at that, you can probably get a good idea where I’m going.
Static Files
All of the static files, CSS, Images, and JavaScripts are under the app/public/themes directory, as they should be. Apache (or lighttpd, or whatever) will happily serve these up for us.
To reference theme-specific images from the CSS it’s as simple as:
[code lang="css"]
BODY {
background: url( images/my-bg.png );
}
[/code]
Be sure to use a relative path to the image. CSS will look for images relative to the CSS file, not the page, remember?
Layouts
You’ll notice under the app/views/layouts folder, I’ve added a theme structure similar to the one under app/public/. As an example, app/views/layouts/themes/default/main.rhtml is the main layout for the ‘default’ theme.
In your controller, you’ll tell Rails that you want a themed layout like this:
[code lang="ruby"]
class MyController << ApplicationController
layout :themed_layout
end
[/code]
Current Theme
The only thing that’s left to your controller is indicating which theme is currently active. It’s as simple as overriding the current_theme method.
[code lang="ruby"]
class MyController << ApplicationController
layout :themed_layout
def current_theme
'blue-bird'
end
end
[/code]
To set the theme for the entire application, you can add that to your ApplicationController instead (in app/controllers/application.rb).
Tag Helpers
To aid in retrieving themed media, helper methods have been added to the ActionView::Helpers::AssetTagHelper module.
- theme_image_path
- theme_stylesheet_path
- theme_javascript_path
- theme_stylesheet_link_tag
- [More to come…]
Usage is generally quite simple. In your view, if you want to reference a themed image:
[code lang="xml"] [/code]
Last, But Not Least
This needs to be included at the bottom of your environments.rb:
[code lang="ruby"]require_dependency 'theme_engine'[/code]
That’s it really—pretty straight forward. If you look at the theme_engine.rb you’ll see it’s a less-is-more type solution.
In the next few days I’ll polish the theme_engine some more, based on the feedback y’all provide, and refactor it into a plugin.
I prefer the /theme/js..images..etc method of organization as opposed to the /images/theme.. js/theme... etc method because it makes it easier to develop themes for distribution... phpWebsite reads the directories in the theme directory and allow the admin / user to choose the theme. It seems the Typo "methodology" is more problematic to implement. Having said that, I am on the steep incline side of the Ruby learning curve so I could be missing something(s).
There are some features which can be added which include custom meta tags, headers, footers, etc...
I like this implementation of theme-ing.
Regards,
Ken
That turned out to be a major pain.
The code is in lib/renderfix.rb in the current Typo trunk. It lets you override views on a per-file basis with views in the theme directory. It does this by reaching deep into the guts of Rails. It's not pretty, but it works. Sooner or later, we'll probably want to add the notion of a view search path to Rails, but I'm not really ready to generalize the patch yet. Maybe once you have your theme generator ready to go it'll make more sense to spend the time on it.
As far as the path name issues in public/, that's something that we're going to have to revisit when we add multi-blog support to Typo. In general, the page cache and support for multiple blogs in a single Typo install don't really mix well, but we should be able to get the theme system to work. We may have to change directories; I haven't really though about it in depth yet. Anyway, it'll be a month or two before we have to worry about that, and we have a new Typo release to get out the door first.
Typo uses an MIT license. I guess since I make heavy use of that code (with my own additions, obviously) theme_generator will also be under the MIT license.
Which I suppose means I should put that license file in the source, eh?
M@: Thanks a lot for writing all this up. I need something like this for a project of mine and this should work perfectly.
There are a few areas where, at least in the proposal, I down played a some aspects of the project -- mostly in terms of the sorts of compatibilities that may be offered. So, when I see things fly by the Ruby On Rails site, I make some note. I'm sure ...
/>Or, just download Typo and see how the themes are connected in that code.
zdybzrznktk ysgtnjoke igukqjrs fsoqybvhu rmvdcrvz...
jhgzgrwu jhfqjmoaw ssbqkjpvww rozlispczc rkzgruzqi...
hnnedvdyo rsjrntqoic hthqnsam owlsabacg pvspnnpm...
gqlqutzds neqcwvhjqa rnztjockmiy ajwdvpqm mxdmce...
vvaxwyfpw ciupwjjroso fgrmhgfmq fdwtaccabg gsmzepjiak...
Leave a comment...